Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ShippingAddressManagement.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Quote\Model;
8 
12 use Psr\Log\LoggerInterface as Logger;
13 
19 {
25  protected $quoteRepository;
26 
32  protected $logger;
33 
39  protected $addressValidator;
40 
44  protected $addressRepository;
45 
49  protected $scopeConfig;
50 
54  protected $totalsCollector;
55 
65  public function __construct(
68  Logger $logger,
72  ) {
73  $this->quoteRepository = $quoteRepository;
74  $this->addressValidator = $addressValidator;
75  $this->logger = $logger;
76  $this->addressRepository = $addressRepository;
77  $this->scopeConfig = $scopeConfig;
78  $this->totalsCollector = $totalsCollector;
79  }
80 
85  public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address)
86  {
88  $quote = $this->quoteRepository->getActive($cartId);
89  if ($quote->isVirtual()) {
90  throw new NoSuchEntityException(
91  __('The Cart includes virtual product(s) only, so a shipping address is not used.')
92  );
93  }
94 
95  $saveInAddressBook = $address->getSaveInAddressBook() ? 1 : 0;
96  $sameAsBilling = $address->getSameAsBilling() ? 1 : 0;
97  $customerAddressId = $address->getCustomerAddressId();
98  $this->addressValidator->validate($address);
99  $quote->setShippingAddress($address);
100  $address = $quote->getShippingAddress();
101 
102  if ($customerAddressId === null) {
103  $address->setCustomerAddressId(null);
104  }
105 
106  if ($customerAddressId) {
107  $addressData = $this->addressRepository->getById($customerAddressId);
108  $address = $quote->getShippingAddress()->importCustomerAddressData($addressData);
109  } elseif ($quote->getCustomerId()) {
110  $address->setEmail($quote->getCustomerEmail());
111  }
112  $address->setSameAsBilling($sameAsBilling);
113  $address->setSaveInAddressBook($saveInAddressBook);
114  $address->setCollectShippingRates(true);
115 
116  try {
117  $address->save();
118  } catch (\Exception $e) {
119  $this->logger->critical($e);
120  throw new InputException(__('The address failed to save. Verify the address and try again.'));
121  }
122  return $quote->getShippingAddress()->getId();
123  }
124 
128  public function get($cartId)
129  {
131  $quote = $this->quoteRepository->getActive($cartId);
132  if ($quote->isVirtual()) {
133  throw new NoSuchEntityException(
134  __('The Cart includes virtual product(s) only, so a shipping address is not used.')
135  );
136  }
138  return $quote->getShippingAddress();
139  }
140 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$quote
__()
Definition: __.php:13
$address
Definition: customer.php:38
$addressData
Definition: order.php:19
$cartId
Definition: quote.php:22
assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address)
__construct(\Magento\Quote\Api\CartRepositoryInterface $quoteRepository, QuoteAddressValidator $addressValidator, Logger $logger, \Magento\Customer\Api\AddressRepositoryInterface $addressRepository, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Quote\Model\Quote\TotalsCollector $totalsCollector)