/** * @param array $productsData * @dataProvider generateSimpleProductsWithPartialDataDataProvider * @magentoDbIsolation enabled */ public function testGenerateSimpleProductsWithPartialData($productsData) { $this->_product->setNewVariationsAttributeSetId(4); $generatedProducts = $this->_model->generateSimpleProducts($this->_product, $productsData); $parentStockItem = $this->stockRegistry->getStockItem($this->_product->getId()); foreach ($generatedProducts as $productId) { $stockItem = $this->stockRegistry->getStockItem($productId); $this->assertEquals($parentStockItem->getManageStock(), $stockItem->getManageStock()); $this->assertEquals('1', $stockItem->getIsInStock()); } }
/** * @param array $productsData * @dataProvider generateSimpleProductsWithPartialDataDataProvider * @magentoDbIsolation enabled */ public function testGenerateSimpleProductsWithPartialData($productsData) { $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); /** @var \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry */ $stockRegistry = $objectManager->get('Magento\\CatalogInventory\\Api\\StockRegistryInterface'); $this->_product->setNewVariationsAttributeSetId(4); $generatedProducts = $this->_model->generateSimpleProducts($this->_product, $productsData); foreach ($generatedProducts as $productId) { $stockItem = $stockRegistry->getStockItem($productId); $this->assertEquals('0', $stockItem->getManageStock()); $this->assertEquals('1', $stockItem->getIsInStock()); } }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testGenerateSimpleProducts() { $productsData = [6 => ['image' => 'image.jpg', 'name' => 'config-red', 'configurable_attribute' => '{"new_attr":"6"}', 'sku' => 'config-red', 'quantity_and_stock_status' => ['qty' => ''], 'weight' => '333']]; $stockData = ['manage_stock' => '0', 'use_config_enable_qty_increments' => '1', 'use_config_qty_increments' => '1', 'use_config_manage_stock' => 0, 'is_decimal_divided' => 0]; $parentProductMock = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['__wakeup', 'getNewVariationsAttributeSetId', 'getStockData', 'getQuantityAndStockStatus', 'getWebsiteIds'])->disableOriginalConstructor()->getMock(); $newSimpleProductMock = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['__wakeup', 'save', 'getId', 'setStoreId', 'setTypeId', 'setAttributeSetId', 'getTypeInstance', 'getStoreId', 'addData', 'setWebsiteIds', 'setStatus', 'setVisibility'])->disableOriginalConstructor()->getMock(); $productTypeMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product\\Type')->setMethods(['getSetAttributes'])->disableOriginalConstructor()->getMock(); $editableAttributeMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute')->setMethods(['getIsUnique', 'getAttributeCode', 'getFrontend', 'getIsVisible'])->disableOriginalConstructor()->getMock(); $frontendAttributeMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute\\Frontend')->setMethods(['getInputType'])->disableOriginalConstructor()->getMock(); $parentProductMock->expects($this->once())->method('getNewVariationsAttributeSetId')->willReturn('new_attr_set_id'); $this->productFactoryMock->expects($this->once())->method('create')->willReturn($newSimpleProductMock); $newSimpleProductMock->expects($this->once())->method('setStoreId')->with(0)->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('setTypeId')->with('simple')->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('setAttributeSetId')->with('new_attr_set_id')->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('getTypeInstance')->willReturn($productTypeMock); $productTypeMock->expects($this->once())->method('getSetAttributes')->with($newSimpleProductMock)->willReturn([$editableAttributeMock]); $editableAttributeMock->expects($this->once())->method('getIsUnique')->willReturn(false); $editableAttributeMock->expects($this->once())->method('getAttributeCode')->willReturn('some_code'); $editableAttributeMock->expects($this->any())->method('getFrontend')->willReturn($frontendAttributeMock); $frontendAttributeMock->expects($this->any())->method('getInputType')->willReturn('input_type'); $editableAttributeMock->expects($this->any())->method('getIsVisible')->willReturn(false); $parentProductMock->expects($this->once())->method('getStockData')->willReturn($stockData); $parentProductMock->expects($this->once())->method('getQuantityAndStockStatus')->willReturn(['is_in_stock' => 1]); $newSimpleProductMock->expects($this->once())->method('getStoreId')->willReturn('store_id'); $this->stockConfiguration->expects($this->once())->method('getManageStock')->with('store_id')->willReturn(1); $newSimpleProductMock->expects($this->once())->method('addData')->willReturnSelf(); $parentProductMock->expects($this->once())->method('getWebsiteIds')->willReturn('website_id'); $newSimpleProductMock->expects($this->once())->method('setWebsiteIds')->with('website_id')->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('setVisibility')->with(1)->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('save')->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('getId')->willReturn('product_id'); $this->assertEquals(['product_id'], $this->model->generateSimpleProducts($parentProductMock, $productsData)); }
/** * Initialize data for configurable product * * @param \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject * @param \Magento\Catalog\Model\Product $product * * @return \Magento\Catalog\Model\Product * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterInitialize(\Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject, \Magento\Catalog\Model\Product $product) { $attributes = $this->request->getParam('attributes'); if ($product->getTypeId() == ConfigurableProduct::TYPE_CODE && !empty($attributes)) { $setId = $this->request->getPost('new-variations-attribute-set-id'); $product->setAttributeSetId($setId); $this->productType->setUsedProductAttributeIds($attributes, $product); $product->setNewVariationsAttributeSetId($setId); $associatedProductIds = $this->request->getPost('associated_product_ids', []); $variationsMatrix = $this->request->getParam('variations-matrix', []); if (!empty($variationsMatrix)) { $generatedProductIds = $this->variationHandler->generateSimpleProducts($product, $variationsMatrix); $associatedProductIds = array_merge($associatedProductIds, $generatedProductIds); } $product->setAssociatedProductIds(array_filter($associatedProductIds)); $product->setCanSaveConfigurableAttributes((bool) $this->request->getPost('affect_configurable_product_attributes')); } return $product; }
/** * Relate simple products to configurable * * @param ProductInterface $product * @param ProductExtensionInterface $extensionAttributes * @return void * @throws \Magento\Framework\Exception\LocalizedException */ private function setLinkedProducts(ProductInterface $product, ProductExtensionInterface $extensionAttributes) { $associatedProductIds = $this->request->getPost('associated_product_ids', []); $variationsMatrix = $this->request->getParam('variations-matrix', []); if (!empty($variationsMatrix)) { $generatedProductIds = $this->variationHandler->generateSimpleProducts($product, $variationsMatrix); $associatedProductIds = array_merge($associatedProductIds, $generatedProductIds); } $extensionAttributes->setConfigurableProductLinks(array_filter($associatedProductIds)); }
/** * Relate simple products to configurable * * @param ProductInterface $product * @param ProductExtensionInterface $extensionAttributes * @return void * @throws \Magento\Framework\Exception\LocalizedException */ private function setLinkedProducts(ProductInterface $product, ProductExtensionInterface $extensionAttributes) { $associatedProductIds = $product->hasData('associated_product_ids') ? $product->getData('associated_product_ids') : []; $variationsMatrix = $this->getVariationMatrixFromProduct($product); if ($associatedProductIds || $variationsMatrix) { $this->variationHandler->prepareAttributeSet($product); } if (!empty($variationsMatrix)) { $generatedProductIds = $this->variationHandler->generateSimpleProducts($product, $variationsMatrix); $associatedProductIds = array_merge($associatedProductIds, $generatedProductIds); } $extensionAttributes->setConfigurableProductLinks(array_filter($associatedProductIds)); }