/** * {@inheritdoc} */ public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem) { $qty = $cartItem->getQty(); if (!is_numeric($qty) || $qty <= 0) { throw InputException::invalidFieldValue('qty', $qty); } $cartId = $cartItem->getQuoteId(); /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); $itemId = $cartItem->getItemId(); try { /** update item qty */ if (isset($itemId)) { $cartItem = $quote->getItemById($itemId); if (!$cartItem) { throw new NoSuchEntityException(__('Cart %1 doesn\'t contain item %2', $cartId, $itemId)); } $product = $this->productRepository->get($cartItem->getSku()); $cartItem->setData('qty', $qty); } else { $product = $this->productRepository->get($cartItem->getSku()); $quote->addProduct($product, $qty); } $this->quoteRepository->save($quote->collectTotals()); } catch (\Exception $e) { if ($e instanceof NoSuchEntityException) { throw $e; } throw new CouldNotSaveException(__('Could not save quote')); } return $quote->getItemByProduct($product); }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem) { $qty = $cartItem->getQty(); if (!is_numeric($qty) || $qty <= 0) { throw InputException::invalidFieldValue('qty', $qty); } $cartId = $cartItem->getQuoteId(); /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); $itemId = $cartItem->getItemId(); try { /** update item */ if (isset($itemId)) { $item = $quote->getItemById($itemId); if (!$item) { throw new NoSuchEntityException(__('Cart %1 doesn\'t contain item %2', $cartId, $itemId)); } $productType = $item->getProduct()->getTypeId(); $buyRequestData = $this->getBuyRequest($productType, $cartItem); if (is_object($buyRequestData)) { /** update item product options */ /** @var \Magento\Quote\Model\Quote\Item $cartItem */ $cartItem = $quote->updateItem($itemId, $buyRequestData); } else { /** update item qty */ $item->setData('qty', $qty); } } else { /** add item to shopping cart */ $product = $this->productRepository->get($cartItem->getSku()); $productType = $product->getTypeId(); /** @var \Magento\Quote\Model\Quote\Item|string $cartItem */ $cartItem = $quote->addProduct($product, $this->getBuyRequest($productType, $cartItem)); if (is_string($cartItem)) { throw new \Magento\Framework\Exception\LocalizedException(__($cartItem)); } } $this->quoteRepository->save($quote->collectTotals()); } catch (\Exception $e) { if ($e instanceof NoSuchEntityException || $e instanceof LocalizedException) { throw $e; } throw new CouldNotSaveException(__('Could not save quote')); } $itemId = $cartItem->getId(); foreach ($quote->getAllItems() as $quoteItem) { if ($itemId == $quoteItem->getId()) { $cartItem = $this->addProductOptions($productType, $quoteItem); return $this->applyCustomOptions($cartItem); } } throw new CouldNotSaveException(__('Could not save quote')); }
/** * @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')); }