Tips & Tutorials

How to create a configurable product programmatically in Magento 2

This blog will help you to create a configurable product programmatically in Magento 2

Overview:

  • Step 1: Create simple products
  • Step 2: Create a configurable product
  • Step 3: Assign simple products to the configurable product

Step 1: Create simple products

Firstly, you have to construct the product model so that you can set data for a new product.

protected $productFactory;

public function __construct (

  \Magento\Catalog\Model\ProductFactory $productFactory

)

{

  $this->productFactory = $productFactory;

}

Then create some simple products

  $simpleProduct1 = $this->productFactory->create();

  $simpleProduct1->setData(‘sku’, ‘Test Simple Product 1’);

  $simpleProduct1->setData(‘name’, ‘Test Simple Product 1’);

  $simpleProduct1->setData(‘website_ids’, array(1)); // product can be found in main website

  $simpleProduct1->setData(‘attribute_set_id’, 4);

  $simpleProduct1->setData(‘status’, \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);

  $simpleProduct1->setData(‘visibility’, 4);

  $simpleProduct1->setData(‘price’, 12);

  $simpleProduct1->setData(‘type_id’, ‘simple’);

  $simpleProduct1->setData(‘stock_data’, array(  // set product quantity

   ‘use_config_manage_stock’ => 0,

   ‘manage_stock’ => 1,

   ‘is_in_stock’ => 1,

   ‘qty’ => 100

  ));

  $simpleProduct1->save();

  $simpleProductId1 = $simpleProduct1->getId();  // get simple product id so that we can assign it to configurable product by id later

Step 2: Create a configurable product

$configurableProduct = $this->productFactory->create();

$configurableProduct->setData(‘sku’, ‘Configurable Product’);

$configurableProduct->setData(‘name’, ‘Configurable Product’);

$configurableProduct->setData(‘attribute_set_id’, 4);

$configurableProduct->setData(‘status’, \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);

$configurableProduct->setData(‘type_id’, ‘simple’);

$configurableProduct->setData(‘price’, 0);

$configurableProduct->setData(‘website_ids’, array(1));  // set website

$configurableProduct->setData(‘category_ids’, array(2));// set category

$configurableProduct->setData(stock_data, array(

   ‘use_config_manage_stock’ => 0, //’Use config settings’ checkbox

   ‘manage_stock’ => 1, //manage stock

   ‘is_in_stock’ => 1, //Stock Availability

  )

);

$colorAttrId = $configurableProduct>getResource()->getAttribute(‘color’)->getId();

$configurableProduct>getTypeInstance()->setUsedProductAttributeIds(array($colorAttrId), $configurableProduct); //attribute ID of attribute ‘size_general’ in my store

$configurableAttributesData = $configurableProduct>getTypeInstance()->getConfigurableAttributesAsArray($configurableProduct);

$configurableProduct>setCanSaveConfigurableAttributes(true);

$configurableProduct>setConfigurableAttributesData($configurableAttributesData);

$configurableProductsData = array();

$configurableProductsData[$simpleProductId1] = array( // id of a simple product associated with the configurable

  ‘0’ => array(

   ‘label’ => ‘Red’, //attribute label

   ‘attribute_id’ => $colorAttrId, //color attribute id

   ‘value_index’ => ‘193’,

   ‘is_percent’ => 0,

   ‘pricing_value’ => ’10’,

  )

);

$configurableProductsData[$simpleProductId2] = array(

  ‘0’ => array(

   ‘label’ => Blue,

   ‘attribute_id’ => $colorAttrId,

   ‘value_index’ => ‘200’,        

   ‘is_percent’ => 0,

   ‘pricing_value’ => ’20’,

  )

);

$configurableProduct>setConfigurableProductsData($configurableProductsData);

$configurableProduct>save();

$configurableProductId = $configurableProduct->getId();

Step 3: Assign simple products to the configurable product

$simpleProductIds = array($simpleProductId1 , $simpleProductId2);

$configurableProduct = $this->productFactory->create()->load($productId); // Load Configurable Product

$configurableProduct->setAssociatedProductIds($simpleProductIds); // Assign simple product id

$configurableProduct->setCanSaveConfigurableAttributes(true);

$configurableProduct->save();

A graphic design and copywriting enthusiast.

Marketing Executive | Content Manager

Author: Harvey Nguyen

Harvey Nguyen

A graphic design and copywriting enthusiast. Marketing Executive | Content Manager

Related Posts


Leave a comment

Your email address will not be published. Required fields are marked *

2 × two =

At Magenest, we help our customers thrive by offering advanced solutions and working with excellent business partners.

We’re proud to be one of the top Magento 2 developers with capability in building integration solutions with third party business systems and applications.

Categories

Please follow & like us

Facebook
Facebook
Google+
Google+
https://store.magenest.com/blog/create-configurable-product-magento-2">
YouTube
YouTube