/** * @param \Magento\Sales\Model\Quote $quote * @param \Magento\Catalog\Model\Product $product * @param \Magento\Framework\Object $config * @return \Magento\Sales\Model\Quote\Item|string */ public function init(\Magento\Sales\Model\Quote $quote, \Magento\Catalog\Model\Product $product, \Magento\Framework\Object $config) { /** @var \Magento\CatalogInventory\Service\V1\Data\StockItem $stockItemDo */ $stockItemDo = $this->stockItemService->getStockItem($product->getId()); if ($stockItemDo->getStockId() && $stockItemDo->getIsQtyDecimal()) { $product->setIsQtyDecimal(1); } else { $config->setQty((int) $config->getQty()); } $product->setCartQty($config->getQty()); $item = $quote->addProduct($product, $config, \Magento\Catalog\Model\Product\Type\AbstractType::PROCESS_MODE_FULL); return $item; }
public function testAddProductItemPreparation() { $itemMock = $this->getMock('\\Magento\\Sales\\Model\\Quote\\Item', [], [], '', false); $expectedResult = $itemMock; $requestMock = $this->getMock('\\Magento\\Framework\\Object'); $this->objectFactoryMock->expects($this->once())->method('create')->with($this->equalTo(['qty' => 1]))->will($this->returnValue($requestMock)); $typeInstanceMock = $this->getMock('Magento\\Catalog\\Model\\Product\\Type\\Simple', ['prepareForCartAdvanced'], [], '', false); $productMock = $this->getMock('Magento\\Catalog\\Model\\Product', ['getParentProductId', 'setStickWithinParent', '__wakeup'], [], '', false); $collectionMock = $this->getMock('Magento\\Sales\\Model\\Resource\\Quote\\Item\\Collection', [], [], '', false); $itemMock->expects($this->any())->method('representProduct')->will($this->returnValue(true)); $iterator = new \ArrayIterator([$itemMock]); $collectionMock->expects($this->any())->method('getIterator')->will($this->returnValue($iterator)); $this->quoteItemCollectionFactoryMock->expects($this->once())->method('create')->will($this->returnValue($collectionMock)); $typeInstanceMock->expects($this->once())->method('prepareForCartAdvanced')->will($this->returnValue([$productMock])); $this->productMock->expects($this->once())->method('getTypeInstance')->will($this->returnValue($typeInstanceMock)); $result = $this->quote->addProduct($this->productMock, null); $this->assertEquals($expectedResult, $result); }
/** * Add products to quote * * @param \Magento\Sales\Model\Quote $quote * @param array $itemsData * @return $this */ protected function addProductToQuote($quote, $itemsData) { foreach ($itemsData as $itemData) { $sku = $itemData['sku']; $price = $itemData['price']; $qty = isset($itemData['qty']) ? $itemData['qty'] : 1; $taxClassName = isset($itemData['tax_class_name']) ? $itemData['tax_class_name'] : self::PRODUCT_TAX_CLASS_1; $taxClassId = $this->productTaxClasses[$taxClassName]; $product = $this->createSimpleProduct($sku, $price, $taxClassId); $quote->addProduct($product, $qty); } return $this; }