Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Processor.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Quote\Model\Quote\ItemFactory;
15 
21 class Processor
22 {
26  protected $quoteItemFactory;
27 
31  protected $storeManager;
32 
36  protected $appState;
37 
43  public function __construct(
44  ItemFactory $quoteItemFactory,
47  ) {
48  $this->quoteItemFactory = $quoteItemFactory;
49  $this->storeManager = $storeManager;
50  $this->appState = $appState;
51  }
52 
62  {
63  $item = $this->quoteItemFactory->create();
64 
65  $this->setItemStoreId($item);
66 
70  if ($item->getId() && $product->getParentProductId()) {
71  return $item;
72  }
73 
74  if ($request->getResetCount() && !$product->getStickWithinParent() && $item->getId() === $request->getId()) {
75  $item->setData(CartItemInterface::KEY_QTY, 0);
76  }
77 
78  return $item;
79  }
80 
89  public function prepare(Item $item, DataObject $request, Product $candidate): void
90  {
94  if ($request->getResetCount() && !$candidate->getStickWithinParent() && $item->getId() == $request->getId()) {
95  $item->setData(CartItemInterface::KEY_QTY, 0);
96  }
97  $item->addQty($candidate->getCartQty());
98 
99  $customPrice = $request->getCustomPrice();
100  if (!empty($customPrice)) {
101  $item->setCustomPrice($customPrice);
102  $item->setOriginalCustomPrice($customPrice);
103  }
104  }
105 
115  public function merge(Item $source, Item $target): Item
116  {
117  return $target;
118  }
119 
126  protected function setItemStoreId(Item $item): void
127  {
128  if ($this->appState->getAreaCode() === \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE) {
129  $storeId = $this->storeManager->getStore($this->storeManager->getStore()->getId())
130  ->getId();
131  $item->setStoreId($storeId);
132  } else {
133  $item->setStoreId($this->storeManager->getStore()->getId());
134  }
135  }
136 }
$source
Definition: source.php:23
$target
Definition: skip.phtml:8
merge(Item $source, Item $target)
Definition: Processor.php:115
init(Product $product, DataObject $request)
Definition: Processor.php:61
prepare(Item $item, DataObject $request, Product $candidate)
Definition: Processor.php:89
__construct(ItemFactory $quoteItemFactory, StoreManagerInterface $storeManager, State $appState)
Definition: Processor.php:43