/**
  * @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);
 }
예제 #2
0
 /**
  * @param CartInterface $quote
  * @param CartItemInterface $item
  * @return CartItemInterface
  * @throws CouldNotSaveException
  * @throws InputException
  * @throws LocalizedException
  * @throws NoSuchEntityException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function save(CartInterface $quote, CartItemInterface $item)
 {
     /** @var \Magento\Quote\Model\Quote $quote */
     $qty = $item->getQty();
     if (!is_numeric($qty) || $qty <= 0) {
         throw InputException::invalidFieldValue('qty', $qty);
     }
     $cartId = $item->getQuoteId();
     $itemId = $item->getItemId();
     try {
         /** Update existing item */
         if (isset($itemId)) {
             $currentItem = $quote->getItemById($itemId);
             if (!$currentItem) {
                 throw new NoSuchEntityException(__('Cart %1 does not contain item %2', $cartId, $itemId));
             }
             $productType = $currentItem->getProduct()->getTypeId();
             $buyRequestData = $this->cartItemOptionProcessor->getBuyRequest($productType, $item);
             if (is_object($buyRequestData)) {
                 /** Update item product options */
                 $item = $quote->updateItem($itemId, $buyRequestData);
             } else {
                 if ($item->getQty() !== $currentItem->getQty()) {
                     $currentItem->setQty($qty);
                 }
             }
         } else {
             /** add new item to shopping cart */
             $product = $this->productRepository->get($item->getSku());
             $productType = $product->getTypeId();
             $item = $quote->addProduct($product, $this->cartItemOptionProcessor->getBuyRequest($productType, $item));
             if (is_string($item)) {
                 throw new LocalizedException(__($item));
             }
         }
     } catch (NoSuchEntityException $e) {
         throw $e;
     } catch (LocalizedException $e) {
         throw $e;
     } catch (\Exception $e) {
         throw new CouldNotSaveException(__('Could not save quote'));
     }
     $itemId = $item->getId();
     foreach ($quote->getAllItems() as $quoteItem) {
         /** @var \Magento\Quote\Model\Quote\Item $quoteItem */
         if ($itemId == $quoteItem->getId()) {
             $item = $this->cartItemOptionProcessor->addProductOptions($productType, $quoteItem);
             return $this->cartItemOptionProcessor->applyCustomOptions($item);
         }
     }
     throw new CouldNotSaveException(__('Could not save quote'));
 }