/** * @param \Magento\Quote\Model\Quote $quote * @param \Magento\Catalog\Model\Product $product * @param \Magento\Framework\DataObject $config * @return \Magento\Quote\Model\Quote\Item|string */ public function init(\Magento\Quote\Model\Quote $quote, \Magento\Catalog\Model\Product $product, \Magento\Framework\DataObject $config) { $stockItem = $this->stockRegistry->getStockItem($product->getId(), $quote->getStore()->getWebsiteId()); if ($stockItem->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\\Quote\\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\\Quote\\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); }
private function initializeQuoteItems() { foreach ($this->proxyOrder->getItems() as $item) { $this->clearQuoteItemsCache(); /** @var $quoteItemBuilder \Ess\M2ePro\Model\Magento\Quote\Item */ $quoteItemBuilder = $this->modelFactory->getObject('Magento\\Quote\\Item', ['quote' => $this->quote, 'proxyItem' => $item]); $product = $quoteItemBuilder->getProduct(); $request = $quoteItemBuilder->getRequest(); // --------------------------------------- $productOriginalPrice = (double) $product->getPrice(); $price = $item->getBasePrice(); $product->setPrice($price); $product->setSpecialPrice($price); // --------------------------------------- // see Mage_Sales_Model_Observer::substractQtyFromQuotes $this->quote->setItemsCount($this->quote->getItemsCount() + 1); $this->quote->setItemsQty((double) $this->quote->getItemsQty() + $request->getQty()); $result = $this->quote->addProduct($product, $request); if (is_string($result)) { throw new \Ess\M2ePro\Model\Exception($result); } $quoteItem = $this->quote->getItemByProduct($product); if ($quoteItem !== false) { $quoteItem->setStoreId($this->quote->getStoreId()); $quoteItem->setOriginalCustomPrice($item->getPrice()); $quoteItem->setOriginalPrice($productOriginalPrice); $quoteItem->setBaseOriginalPrice($productOriginalPrice); $quoteItem->setNoDiscount(1); $giftMessageId = $quoteItemBuilder->getGiftMessageId(); if (!empty($giftMessageId)) { $quoteItem->setGiftMessageId($giftMessageId); } $quoteItem->setAdditionalData($quoteItemBuilder->getAdditionalData($quoteItem)); } } }
/** * Add products to quote * * @param \Magento\Quote\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; }
/** * Add existing or newly created products to the quote * * @param \Magento\Quote\Model\Quote $quote */ protected function addItemsToQuote($quote) { /* Check if there are some generated sample products */ $product = $this->productFactory->create(); $productsCollection = $product->getCollection()->addAttributeToFilter('name', ['like' => self::NAMES_PREFIX . '%']); if ($productsCollection->getSize() > 0) { $product = $productsCollection->getFirstItem(); } else { /* If there are no generated products - try to create one */ $product = $this->productsCreator->createSimpleProduct(); } $quote->addProduct($product); }
public function _populateQuoteItems(\Magento\Quote\Model\Quote $quote) { /** @var \Magento\Catalog\Model\Product $product */ $product = $this->_getProductBySku(self::SKU); // $product->setPrice(8.00); $req = new \Magento\Framework\DataObject(); $req->setQty(1); $req->setCustomPrice(8); $quote->addProduct($product, $req); return $quote; }
/** * Add product to quote * * This file was inspired by * @see dev/tests/integration/testsuite/Magento/Checkout/_files/quote_with_bundle_and_options.php * * @param \Magento\Quote\Model\Quote $quote * @param \Magento\Catalog\Model\Product $product * @param int $qty * @param $itemData * @throws \Exception * @throws \Magento\Framework\Exception\LocalizedException */ protected function addProductToQuote(\Magento\Quote\Model\Quote $quote, \Magento\Catalog\Model\Product $product, $qty, $itemData) { if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE) { $quote->addProduct($product, $qty); } elseif ($product->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) { $attribute = $this->getConfigurableAttribute(self::CONFIGURABLE_ATTRIBUTE_NAME); /** @var \Magento\Eav\Api\Data\AttributeOptionInterface[] $options */ $options = $attribute->getOptions(); array_shift($options); //remove the first option which is empty $requestInfo = new \Magento\Framework\DataObject(); if (!empty($options)) { $option = $options[0]; $requestData = ['qty' => $qty]; /** @var \Magento\ConfigurableProduct\Api\Data\ConfigurableItemOptionValueInterface $option */ $requestData['super_attribute'][$attribute->getId()] = $option->getValue(); $requestInfo->addData($requestData); } $quote->addProduct($product, $requestInfo); } elseif ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) { /** @var $typeInstance \Magento\Bundle\Model\Product\Type */ //Load options $typeInstance = $product->getTypeInstance(); $typeInstance->setStoreFilter($product->getStoreId(), $product); $optionCollection = $typeInstance->getOptionsCollection($product); $bundleOptions = []; $bundleOptionsQty = []; /** @var $option \Magento\Bundle\Model\Option */ foreach ($optionCollection as $option) { $selectionsCollection = $typeInstance->getSelectionsCollection([$option->getId()], $product); if ($option->isMultiSelection()) { $selectionsCollection->load(); $bundleOptions[$option->getId()] = array_column($selectionsCollection->toArray(), 'selection_id'); } else { $bundleOptions[$option->getId()] = $selectionsCollection->getFirstItem()->getSelectionId(); } $optionQty = 1; foreach ($itemData['bundled_options'] as $bundledOptionData) { if ($option->getTitle() == $bundledOptionData['title']) { $optionQty = $bundledOptionData['qty']; break; } } $bundleOptionsQty[$option->getId()] = $optionQty; } $requestInfo = new \Magento\Framework\DataObject(['qty' => $qty, 'bundle_option' => $bundleOptions, 'bundle_option_qty' => $bundleOptionsQty]); $quote->addProduct($product, $requestInfo); } else { throw new \Exception('Unrecognized type: ' . $product->getTypeId()); } }