/** * Initialize quote item object * * @param \Magento\Framework\Object $request * @param Product $product * * @return \Magento\Quote\Model\Quote\Item */ public function init(Product $product, $request) { $item = $this->quoteItemFactory->create(); $this->setItemStoreId($item); /** * We can't modify existing child items */ if ($item->getId() && $product->getParentProductId()) { return $item; } $item->setOptions($product->getCustomOptions()); $item->setProduct($product); if ($request->getResetCount() && !$product->getStickWithinParent() && $item->getId() === $request->getId()) { $item->setData('qty', 0); } return $item; }
/** * Adding catalog product object data to quote * * @param \Magento\Catalog\Model\Product $product * @param int $qty * @return \Magento\Quote\Model\Quote\Item * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ protected function _addCatalogProduct(\Magento\Catalog\Model\Product $product, $qty = 1) { $newItem = false; $item = $this->getItemByProduct($product); if (!$item) { $item = $this->_quoteItemFactory->create(); $item->setQuote($this); if ($this->_appState->getAreaCode() === \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE) { $item->setStoreId($this->getStore()->getId()); } else { $item->setStoreId($this->_storeManager->getStore()->getId()); } $newItem = true; } /** * We can't modify existing child items */ if ($item->getId() && $product->getParentProductId()) { return $item; } $item->setOptions($product->getCustomOptions())->setProduct($product); // Add only item that is not in quote already (there can be other new or already saved item if ($newItem) { $this->addItem($item); } return $item; }