Repository.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Quote\Model\Quote\Item;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\Exception\CouldNotSaveException;
  10. use Magento\Framework\Exception\NoSuchEntityException;
  11. class Repository implements \Magento\Quote\Api\CartItemRepositoryInterface
  12. {
  13. /**
  14. * Quote repository.
  15. *
  16. * @var \Magento\Quote\Api\CartRepositoryInterface
  17. */
  18. protected $quoteRepository;
  19. /**
  20. * Product repository.
  21. *
  22. * @var \Magento\Catalog\Api\ProductRepositoryInterface
  23. */
  24. protected $productRepository;
  25. /**
  26. * @var \Magento\Quote\Api\Data\CartItemInterfaceFactory
  27. */
  28. protected $itemDataFactory;
  29. /**
  30. * @var CartItemProcessorInterface[]
  31. */
  32. protected $cartItemProcessors;
  33. /**
  34. * @var CartItemOptionsProcessor
  35. */
  36. private $cartItemOptionsProcessor;
  37. /**
  38. * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
  39. * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  40. * @param \Magento\Quote\Api\Data\CartItemInterfaceFactory $itemDataFactory
  41. * @param CartItemProcessorInterface[] $cartItemProcessors
  42. */
  43. public function __construct(
  44. \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
  45. \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
  46. \Magento\Quote\Api\Data\CartItemInterfaceFactory $itemDataFactory,
  47. array $cartItemProcessors = []
  48. ) {
  49. $this->quoteRepository = $quoteRepository;
  50. $this->productRepository = $productRepository;
  51. $this->itemDataFactory = $itemDataFactory;
  52. $this->cartItemProcessors = $cartItemProcessors;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function getList($cartId)
  58. {
  59. $output = [];
  60. /** @var \Magento\Quote\Model\Quote $quote */
  61. $quote = $this->quoteRepository->getActive($cartId);
  62. /** @var \Magento\Quote\Model\Quote\Item $item */
  63. foreach ($quote->getAllVisibleItems() as $item) {
  64. $item = $this->getCartItemOptionsProcessor()->addProductOptions($item->getProductType(), $item);
  65. $output[] = $this->getCartItemOptionsProcessor()->applyCustomOptions($item);
  66. }
  67. return $output;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
  73. {
  74. /** @var \Magento\Quote\Model\Quote $quote */
  75. $cartId = $cartItem->getQuoteId();
  76. $quote = $this->quoteRepository->getActive($cartId);
  77. $quoteItems = $quote->getItems();
  78. $quoteItems[] = $cartItem;
  79. $quote->setItems($quoteItems);
  80. $this->quoteRepository->save($quote);
  81. $quote->collectTotals();
  82. return $quote->getLastAddedItem();
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function deleteById($cartId, $itemId)
  88. {
  89. /** @var \Magento\Quote\Model\Quote $quote */
  90. $quote = $this->quoteRepository->getActive($cartId);
  91. $quoteItem = $quote->getItemById($itemId);
  92. if (!$quoteItem) {
  93. throw new NoSuchEntityException(
  94. __('The %1 Cart doesn\'t contain the %2 item.', $cartId, $itemId)
  95. );
  96. }
  97. try {
  98. $quote->removeItem($itemId);
  99. $this->quoteRepository->save($quote);
  100. } catch (\Exception $e) {
  101. throw new CouldNotSaveException(__("The item couldn't be removed from the quote."));
  102. }
  103. return true;
  104. }
  105. /**
  106. * @return CartItemOptionsProcessor
  107. * @deprecated 100.1.0
  108. */
  109. private function getCartItemOptionsProcessor()
  110. {
  111. if (!$this->cartItemOptionsProcessor instanceof CartItemOptionsProcessor) {
  112. $this->cartItemOptionsProcessor = ObjectManager::getInstance()->get(CartItemOptionsProcessor::class);
  113. }
  114. return $this->cartItemOptionsProcessor;
  115. }
  116. }