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;
81 private $cartExtensionFactory;
91 private $shippingFactory;
120 CartExtensionFactory $cartExtensionFactory =
null,
122 ShippingFactory $shippingFactory =
null 134 ->get(CartExtensionFactory::class);
136 ->get(ShippingAssignmentFactory::class);
138 ->get(ShippingFactory::class);
155 $address = $addressInformation->getShippingAddress();
157 $carrierCode = $addressInformation->getShippingCarrierCode();
158 $methodCode = $addressInformation->getShippingMethodCode();
160 if (!
$address->getCustomerAddressId()) {
161 $address->setCustomerAddressId(
null);
169 throw new StateException(
__(
'The shipping address is missing. Set the address and try again.'));
174 $address->setLimitCarrier($carrierCode);
177 $quote->setIsMultiShipping(
false);
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.')
195 throw new NoSuchEntityException(
196 __(
'Carrier with such method not found: %1, %2', $carrierCode,
$methodCode)
217 if (0 ==
$quote->getItemsCount()) {
219 __(
"The shipping method can't be set for an empty cart. Add an item to cart and try again.")
234 $cartExtension =
$quote->getExtensionAttributes();
235 if ($cartExtension ===
null) {
236 $cartExtension = $this->cartExtensionFactory->create();
239 $shippingAssignments = $cartExtension->getShippingAssignments();
240 if (empty($shippingAssignments)) {
241 $shippingAssignment = $this->shippingAssignmentFactory->create();
243 $shippingAssignment = $shippingAssignments[0];
246 $shipping = $shippingAssignment->getShipping();
247 if ($shipping ===
null) {
248 $shipping = $this->shippingFactory->create();
253 $shippingAssignment->setShipping($shipping);
254 $cartExtension->setShippingAssignments([$shippingAssignment]);
255 return $quote->setExtensionAttributes($cartExtension);