CartItemPersister.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model\Quote\Item;
  7. use Magento\Quote\Api\Data\CartInterface;
  8. use Magento\Quote\Api\Data\CartItemInterface;
  9. use Magento\Framework\Exception\CouldNotSaveException;
  10. use Magento\Framework\Exception\InputException;
  11. use Magento\Framework\Exception\NoSuchEntityException;
  12. use Magento\Framework\Exception\LocalizedException;
  13. use Magento\Catalog\Api\ProductRepositoryInterface;
  14. class CartItemPersister
  15. {
  16. /**
  17. * @var ProductRepositoryInterface
  18. */
  19. private $productRepository;
  20. /**
  21. * @var CartItemOptionsProcessor
  22. */
  23. private $cartItemOptionProcessor;
  24. /**
  25. * @param ProductRepositoryInterface $productRepository
  26. * @param CartItemOptionsProcessor $cartItemOptionProcessor
  27. */
  28. public function __construct(
  29. ProductRepositoryInterface $productRepository,
  30. CartItemOptionsProcessor $cartItemOptionProcessor
  31. ) {
  32. $this->productRepository = $productRepository;
  33. $this->cartItemOptionProcessor = $cartItemOptionProcessor;
  34. }
  35. /**
  36. * @param CartInterface $quote
  37. * @param CartItemInterface $item
  38. * @return CartItemInterface
  39. * @throws CouldNotSaveException
  40. * @throws InputException
  41. * @throws LocalizedException
  42. * @throws NoSuchEntityException
  43. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  44. */
  45. public function save(CartInterface $quote, CartItemInterface $item)
  46. {
  47. /** @var \Magento\Quote\Model\Quote $quote */
  48. $qty = $item->getQty();
  49. if (!is_numeric($qty) || $qty <= 0) {
  50. throw InputException::invalidFieldValue('qty', $qty);
  51. }
  52. $cartId = $item->getQuoteId();
  53. $itemId = $item->getItemId();
  54. try {
  55. /** Update existing item */
  56. if (isset($itemId)) {
  57. $currentItem = $quote->getItemById($itemId);
  58. if (!$currentItem) {
  59. throw new NoSuchEntityException(
  60. __('The %1 Cart doesn\'t contain the %2 item.', $cartId, $itemId)
  61. );
  62. }
  63. $productType = $currentItem->getProduct()->getTypeId();
  64. $buyRequestData = $this->cartItemOptionProcessor->getBuyRequest($productType, $item);
  65. if (is_object($buyRequestData)) {
  66. /** Update item product options */
  67. $item = $quote->updateItem($itemId, $buyRequestData);
  68. } else {
  69. if ($item->getQty() !== $currentItem->getQty()) {
  70. $currentItem->setQty($qty);
  71. /**
  72. * Qty validation errors are stored as items message
  73. * @see \Magento\CatalogInventory\Model\Quote\Item\QuantityValidator::validate
  74. */
  75. if (!empty($currentItem->getMessage())) {
  76. throw new LocalizedException(__($currentItem->getMessage()));
  77. }
  78. }
  79. }
  80. } else {
  81. /** add new item to shopping cart */
  82. $product = $this->productRepository->get($item->getSku());
  83. $productType = $product->getTypeId();
  84. $item = $quote->addProduct(
  85. $product,
  86. $this->cartItemOptionProcessor->getBuyRequest($productType, $item)
  87. );
  88. if (is_string($item)) {
  89. throw new LocalizedException(__($item));
  90. }
  91. }
  92. } catch (NoSuchEntityException $e) {
  93. throw $e;
  94. } catch (LocalizedException $e) {
  95. throw $e;
  96. } catch (\Exception $e) {
  97. throw new CouldNotSaveException(__("The quote couldn't be saved."));
  98. }
  99. $itemId = $item->getId();
  100. foreach ($quote->getAllItems() as $quoteItem) {
  101. /** @var \Magento\Quote\Model\Quote\Item $quoteItem */
  102. if ($itemId == $quoteItem->getId()) {
  103. $item = $this->cartItemOptionProcessor->addProductOptions($productType, $quoteItem);
  104. return $this->cartItemOptionProcessor->applyCustomOptions($item);
  105. }
  106. }
  107. throw new CouldNotSaveException(__("The quote couldn't be saved."));
  108. }
  109. }