Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ShippingInformationManagement.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Checkout\Model;
8 
14 use Psr\Log\LoggerInterface as Logger;
16 use Magento\Quote\Api\Data\CartExtensionFactory;
17 use Magento\Quote\Model\ShippingAssignmentFactory;
18 use Magento\Quote\Model\ShippingFactory;
20 
28 {
33 
38 
43 
47  protected $quoteRepository;
48 
52  protected $logger;
53 
58  protected $addressValidator;
59 
64  protected $addressRepository;
65 
70  protected $scopeConfig;
71 
76  protected $totalsCollector;
77 
81  private $cartExtensionFactory;
82 
87 
91  private $shippingFactory;
92 
110  public function __construct(
111  \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement,
112  \Magento\Checkout\Model\PaymentDetailsFactory $paymentDetailsFactory,
116  Logger $logger,
120  CartExtensionFactory $cartExtensionFactory = null,
121  ShippingAssignmentFactory $shippingAssignmentFactory = null,
122  ShippingFactory $shippingFactory = null
123  ) {
124  $this->paymentMethodManagement = $paymentMethodManagement;
125  $this->paymentDetailsFactory = $paymentDetailsFactory;
126  $this->cartTotalsRepository = $cartTotalsRepository;
127  $this->quoteRepository = $quoteRepository;
128  $this->addressValidator = $addressValidator;
129  $this->logger = $logger;
130  $this->addressRepository = $addressRepository;
131  $this->scopeConfig = $scopeConfig;
132  $this->totalsCollector = $totalsCollector;
133  $this->cartExtensionFactory = $cartExtensionFactory ?: ObjectManager::getInstance()
134  ->get(CartExtensionFactory::class);
135  $this->shippingAssignmentFactory = $shippingAssignmentFactory ?: ObjectManager::getInstance()
136  ->get(ShippingAssignmentFactory::class);
137  $this->shippingFactory = $shippingFactory ?: ObjectManager::getInstance()
138  ->get(ShippingFactory::class);
139  }
140 
151  public function saveAddressInformation(
152  $cartId,
153  \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
154  ) {
155  $address = $addressInformation->getShippingAddress();
156  $billingAddress = $addressInformation->getBillingAddress();
157  $carrierCode = $addressInformation->getShippingCarrierCode();
158  $methodCode = $addressInformation->getShippingMethodCode();
159 
160  if (!$address->getCustomerAddressId()) {
161  $address->setCustomerAddressId(null);
162  }
163 
164  if ($billingAddress && !$billingAddress->getCustomerAddressId()) {
165  $billingAddress->setCustomerAddressId(null);
166  }
167 
168  if (!$address->getCountryId()) {
169  throw new StateException(__('The shipping address is missing. Set the address and try again.'));
170  }
171 
173  $quote = $this->quoteRepository->getActive($cartId);
174  $address->setLimitCarrier($carrierCode);
175  $quote = $this->prepareShippingAssignment($quote, $address, $carrierCode . '_' . $methodCode);
176  $this->validateQuote($quote);
177  $quote->setIsMultiShipping(false);
178 
179  if ($billingAddress) {
180  $quote->setBillingAddress($billingAddress);
181  }
182 
183  try {
184  $this->quoteRepository->save($quote);
185  } catch (\Exception $e) {
186  $this->logger->critical($e);
187  throw new InputException(
188  __('The shipping information was unable to be saved. Verify the input data and try again.')
189  );
190  }
191 
192  $shippingAddress = $quote->getShippingAddress();
193 
194  if (!$shippingAddress->getShippingRateByCode($shippingAddress->getShippingMethod())) {
195  throw new NoSuchEntityException(
196  __('Carrier with such method not found: %1, %2', $carrierCode, $methodCode)
197  );
198  }
199 
201  $paymentDetails = $this->paymentDetailsFactory->create();
202  $paymentDetails->setPaymentMethods($this->paymentMethodManagement->getList($cartId));
203  $paymentDetails->setTotals($this->cartTotalsRepository->get($cartId));
204  return $paymentDetails;
205  }
206 
215  protected function validateQuote(\Magento\Quote\Model\Quote $quote)
216  {
217  if (0 == $quote->getItemsCount()) {
218  throw new InputException(
219  __("The shipping method can't be set for an empty cart. Add an item to cart and try again.")
220  );
221  }
222  }
223 
232  private function prepareShippingAssignment(CartInterface $quote, AddressInterface $address, $method)
233  {
234  $cartExtension = $quote->getExtensionAttributes();
235  if ($cartExtension === null) {
236  $cartExtension = $this->cartExtensionFactory->create();
237  }
238 
239  $shippingAssignments = $cartExtension->getShippingAssignments();
240  if (empty($shippingAssignments)) {
241  $shippingAssignment = $this->shippingAssignmentFactory->create();
242  } else {
243  $shippingAssignment = $shippingAssignments[0];
244  }
245 
246  $shipping = $shippingAssignment->getShipping();
247  if ($shipping === null) {
248  $shipping = $this->shippingFactory->create();
249  }
250 
251  $shipping->setAddress($address);
252  $shipping->setMethod($method);
253  $shippingAssignment->setShipping($shipping);
254  $cartExtension->setShippingAssignments([$shippingAssignment]);
255  return $quote->setExtensionAttributes($cartExtension);
256  }
257 }
saveAddressInformation( $cartId, \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation)
$billingAddress
Definition: order.php:25
$quote
$shippingAddress
Definition: order.php:40
__()
Definition: __.php:13
$address
Definition: customer.php:38
$cartId
Definition: quote.php:22
$method
Definition: info.phtml:13
__construct(\Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement, \Magento\Checkout\Model\PaymentDetailsFactory $paymentDetailsFactory, \Magento\Quote\Api\CartTotalRepositoryInterface $cartTotalsRepository, \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, CartExtensionFactory $cartExtensionFactory=null, ShippingAssignmentFactory $shippingAssignmentFactory=null, ShippingFactory $shippingFactory=null)