shippingAssignmentFactory = $shippingAssignmentFactory; $this->shippingProcessor = $shippingProcessor; $this->cartItemPersister = $cartItemPersister; $this->addressRepository = $addressRepository ?: ObjectManager::getInstance()->get(AddressRepositoryInterface::class); } /** * Create shipping assignment * * @param CartInterface $quote * @return \Magento\Quote\Api\Data\ShippingAssignmentInterface */ public function create(CartInterface $quote) { /** @var \Magento\Quote\Model\Quote $quote */ $shippingAddress = $quote->getShippingAddress(); /** @var \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment */ $shippingAssignment = $this->shippingAssignmentFactory->create(); $shippingAssignment->setItems($quote->getItems()); $shippingAssignment->setShipping($this->shippingProcessor->create($shippingAddress)); return $shippingAssignment; } /** * Save shipping assignment * * @param ShippingAssignmentInterface $shippingAssignment * @param CartInterface $quote * @return void * @throws InputException|LocalizedException */ public function save(CartInterface $quote, ShippingAssignmentInterface $shippingAssignment) { /** @var \Magento\Quote\Model\Quote $quote */ foreach ($shippingAssignment->getItems() as $item) { /** @var \Magento\Quote\Model\Quote\Item $item */ if (!$quote->getItemById($item->getItemId()) && !$item->isDeleted()) { $this->cartItemPersister->save($quote, $item); } } $shippingAddress = $shippingAssignment->getShipping()->getAddress(); if ($shippingAddress->getCustomerAddressId()) { try { $this->addressRepository->getById($shippingAddress->getCustomerAddressId()); } catch (NoSuchEntityException $e) { $shippingAddress->setCustomerAddressId(null); } } $this->shippingProcessor->save($shippingAssignment->getShipping(), $quote); } }