Processor.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\Catalog\Model\Product;
  8. use Magento\Quote\Model\Quote\ItemFactory;
  9. use Magento\Quote\Model\Quote\Item;
  10. use Magento\Store\Model\StoreManagerInterface;
  11. use Magento\Framework\App\State;
  12. use Magento\Framework\DataObject;
  13. use Magento\Quote\Api\Data\CartItemInterface;
  14. /**
  15. * Class Processor
  16. * - initializes quote item with store_id and qty data
  17. * - updates quote item qty and custom price data
  18. */
  19. class Processor
  20. {
  21. /**
  22. * @var \Magento\Quote\Model\Quote\ItemFactory
  23. */
  24. protected $quoteItemFactory;
  25. /**
  26. * @var \Magento\Store\Model\StoreManagerInterface
  27. */
  28. protected $storeManager;
  29. /**
  30. * @var \Magento\Framework\App\State
  31. */
  32. protected $appState;
  33. /**
  34. * @param ItemFactory $quoteItemFactory
  35. * @param StoreManagerInterface $storeManager
  36. * @param State $appState
  37. */
  38. public function __construct(
  39. ItemFactory $quoteItemFactory,
  40. StoreManagerInterface $storeManager,
  41. State $appState
  42. ) {
  43. $this->quoteItemFactory = $quoteItemFactory;
  44. $this->storeManager = $storeManager;
  45. $this->appState = $appState;
  46. }
  47. /**
  48. * Initialize quote item object
  49. *
  50. * @param DataObject $request
  51. * @param Product $product
  52. *
  53. * @return Item
  54. */
  55. public function init(Product $product, DataObject $request): Item
  56. {
  57. $item = $this->quoteItemFactory->create();
  58. $this->setItemStoreId($item);
  59. /**
  60. * We can't modify existing child items
  61. */
  62. if ($item->getId() && $product->getParentProductId()) {
  63. return $item;
  64. }
  65. if ($request->getResetCount() && !$product->getStickWithinParent() && $item->getId() === $request->getId()) {
  66. $item->setData(CartItemInterface::KEY_QTY, 0);
  67. }
  68. return $item;
  69. }
  70. /**
  71. * Set qty and custom price for quote item
  72. *
  73. * @param Item $item
  74. * @param DataObject $request
  75. * @param Product $candidate
  76. * @return void
  77. */
  78. public function prepare(Item $item, DataObject $request, Product $candidate): void
  79. {
  80. /**
  81. * We specify qty after we know about parent (for stock)
  82. */
  83. if ($request->getResetCount() && !$candidate->getStickWithinParent() && $item->getId() == $request->getId()) {
  84. $item->setData(CartItemInterface::KEY_QTY, 0);
  85. }
  86. $item->addQty($candidate->getCartQty());
  87. $customPrice = $request->getCustomPrice();
  88. if (!empty($customPrice)) {
  89. $item->setCustomPrice($customPrice);
  90. $item->setOriginalCustomPrice($customPrice);
  91. }
  92. }
  93. /**
  94. * Merge two quote items.
  95. *
  96. * @param Item $source
  97. * @param Item $target
  98. * @return Item
  99. *
  100. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  101. */
  102. public function merge(Item $source, Item $target): Item
  103. {
  104. return $target;
  105. }
  106. /**
  107. * Set store_id value to quote item
  108. *
  109. * @param Item $item
  110. * @return void
  111. */
  112. protected function setItemStoreId(Item $item): void
  113. {
  114. if ($this->appState->getAreaCode() === \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE) {
  115. $storeId = $this->storeManager->getStore($this->storeManager->getStore()->getId())
  116. ->getId();
  117. $item->setStoreId($storeId);
  118. } else {
  119. $item->setStoreId($this->storeManager->getStore()->getId());
  120. }
  121. }
  122. }