0

How to Create Configurable Products Programmatically in Magento 2? can anyone tell me about the solution to how to do this?

Thanks

1

Programmatically create configurable products in magnto2


//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();

Try with it.

  • how to get simple product id? if product is not created – Moin Malek Oct 26 '17 at 13:03
  • @MoinMalek First you need to create simple product which assign under configurable one. – Envision Ecommerce Oct 26 '17 at 13:04
  • 1
    Ok Got it. lets try it – Moin Malek Oct 26 '17 at 13:06
  • 1
    @MoinMalek Yes sure, I was able to do that, If any query let me know. If you got success please mark yes to my answer. Thanks – Envision Ecommerce Oct 26 '17 at 13:08
  • Not working at all – Moin Malek Oct 26 '17 at 13:10
0

Part 1: Create the Configurable Product Step 1: Select the Product Type is Configurable Product Step 2: Select the Product Template Step 3: Complete the Required Fields Step 4: Complete the Remaining Product Details Step 5: Save and Continue Part 2: Add Configurations Step 1: Select the Attributes Step 2: Enter the Attribute Values Step 3: Configure the Images, Price and Quantity Step 4: Generate the Product Configurations Step 5: Publish the Products Step 6: Configure the Shopping Cart Thumbnails (Optional) Step 7: Final, check your result by view the Products in your store

  • I want using Programatocally – Moin Malek Oct 26 '17 at 10:38
  • may be it's working.i have create only simple product programmatically. – Mulay Swapnil Oct 26 '17 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.