How to programmatically create a configurable Magento 2 product
If the simple and configurable products are not assigned together like in the folowing example, there is a reliable solution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
//simple product $simple_product = $this->_objectManager->create('\Magento\Catalog\Model\Product'); $simple_product->setSku('test-simple'); $simple_product->setName('test name simple'); $simple_product->setAttributeSetId(4); $simple_product->setSize_general(193); // value id of S size $simple_product->setStatus(1); $simple_product->setTypeId('simple'); $simple_product->setPrice(10); $simple_product->setWebsiteIds(array(1)); $simple_product->setCategoryIds(array(31)); $simple_product->setStockData(array( 'use_config_manage_stock' => 0, //'Use config settings' checkbox 'manage_stock' => 1, //manage stock 'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart 'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart 'is_in_stock' => 1, //Stock Availability 'qty' => 100 //qty ) ); $simple_product->save(); $simple_product_id = $simple_product->getId(); echo "simple product id: ".$simple_product_id."\n"; //configurable product $configurable_product = $this->_objectManager->create('\Magento\Catalog\Model\Product'); $configurable_product->setSku('test-configurable'); $configurable_product->setName('test name configurable'); $configurable_product->setAttributeSetId(4); $configurable_product->setStatus(1); $configurable_product->setTypeId('configurable'); $configurable_product->setPrice(11); $configurable_product->setWebsiteIds(array(1)); $configurable_product->setCategoryIds(array(31)); $configurable_product->setStockData(array( 'use_config_manage_stock' => 0, //'Use config settings' checkbox 'manage_stock' => 1, //manage stock 'is_in_stock' => 1, //Stock Availability ) ); $configurable_product->getTypeInstance()->setUsedProductAttributeIds(array(152),$configurable_product); //attribute ID of attribute 'size_general' in my store $configurableAttributesData = $configurable_product->getTypeInstance()->getConfigurableAttributesAsArray($configurable_product); $configurable_product->setCanSaveConfigurableAttributes(true); $configurable_product->setConfigurableAttributesData($configurableAttributesData); $configurableProductsData = array(); $configurableProductsData[$simple_product_id] = array( //[$simple_product_id] = id of a simple product associated with this configurable '0' => array( 'label' => 'S', //attribute label 'attribute_id' => '152', //attribute ID of attribute 'size_general' in my store 'value_index' => '193', //value of 'S' index of the attribute 'size_general' 'is_percent' => 0, 'pricing_value' => '10', ) ); $configurable_product->setConfigurableProductsData($configurableProductsData); $configurable_product->save(); echo "configurable product id: ".$configurable_product->getId()."\n"; |
You can review on API functional test aimed at
1 2 3 4 5 6 |
$product = $productFactory->create(['name'=> 'configurable product', ... ]); $configurableOption = $optionFactory->create([]); $linkedProduct = $linkFactory->create([]); $product->getExtensionAttributes()->setConfigurableProductOptions($configurableOption)l $product->getExtensionAttributes()->setConfigurableProductLinks($linkedProduct); $productRepository->save($product) |
It is also necessary to mention that the API does not generate simple product, so there is the need to create in advance.
More tips from Magento 2 Developer’s Cookbook