/** * @param ShippingAssignmentInterface $shippingAssignment * @param CartInterface $quote * @return void * @throws InputException */ public function save(CartInterface $quote, ShippingAssignmentInterface $shippingAssignment) { /** @var \Magento\Quote\Model\Quote $quote */ foreach ($shippingAssignment->getItems() as $item) { if (!$quote->getItemById($item->getItemId())) { $this->cartItemPersister->save($quote, $item); } } $this->shippingProcessor->save($shippingAssignment->getShipping(), $quote); }
/** * Test saving shipping assignments with deleted cart items * * @covers \Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor::save */ public function testSaveWithDeletedCartItems() { $shippingAssignment = $this->getMockForAbstractClass(ShippingAssignmentInterface::class); $shipping = $this->getMockForAbstractClass(ShippingInterface::class); $quoteId = 1; $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock(); $quoteItem = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)->disableOriginalConstructor()->getMock(); $quoteItem->expects(static::once())->method('isDeleted')->willReturn(true); $quoteItem->expects(static::once())->method('getItemId')->willReturn($quoteId); $quote->expects(static::once())->method('getItemById')->with($quoteId)->willReturn(null); $shippingAssignment->expects(static::once())->method('getItems')->willReturn([$quoteItem]); $shippingAssignment->expects(static::once())->method('getShipping')->willReturn($shipping); $this->cartItemPersister->expects(static::never())->method('save'); $this->shippingProcessor->expects(static::once())->method('save')->with($shipping, $quote); $this->shippingAssignmentProcessor->save($quote, $shippingAssignment); }
/** * @param CartInterface $quote * @return CartInterface * * @throws InputException * @throws \Magento\Framework\Exception\CouldNotSaveException * @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function save(CartInterface $quote) { /** @var \Magento\Quote\Model\Quote $quote */ // Quote Item processing $items = $quote->getItems(); if ($items) { foreach ($items as $item) { /** @var \Magento\Quote\Model\Quote\Item $item */ if (!$item->isDeleted()) { $quote->setLastAddedItem($this->cartItemPersister->save($quote, $item)); } } } // Billing Address processing $billingAddress = $quote->getBillingAddress(); if ($billingAddress) { $this->billingAddressPersister->save($quote, $billingAddress); } $this->processShippingAssignment($quote); $this->quoteResourceModel->save($quote->collectTotals()); return $quote; }