Programmatically Create Configurable Product in Magento 2 Simplified
If you are here, then you are a beginner in Magento 2 world! Welcome. It isn’t as hard as people say 😉
Magento 2 configurable product is a single product with variations in it. Each variation is an individual simple product with a unique SKU. Hence, one can track the inventory for each variation.
There are six product types in Magento 2:
- Simple Product
- Configurable Product
- Grouped Product
- Virtual Product
- Bundle Product
- Downloadable Product
Learning how to programmatically create configurable product in Magento 2 makes it easy for the developers while creating an online store.
For example, apparel available in different sizes or colors is an example of a configurable product.
Check the below code to become a pro in creating Magento 2 configurable products!
Method to programmatically create configurable product in Magento 2:
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 |
<?php namespace [Vendor]\[Module]\Controller; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Catalog\Model\Product; class Test extends Action { protected $customerSession; protected $resultPageFactory; protected $redirectFactory; protected $newProduct; public function __construct(Context $context, Product $newProduct, \Magento\Framework\ObjectManagerInterface $objectManager) { $this->newProduct = $newProduct; parent::__construct($context); } public function execute() { $product = $this->newProduct; $product->setSku('mySku'); // Set your sku here $product->setName('Sample Configurable Product'); // Name of Product $product->setAttributeSetId(4); // Attribute set id $product->setStatus(1); // Status on product enabled/ disabled 1/0 $product->setWeight(10); // weight of product $product->setVisibility(4); // visibilty of product (catalog / search / catalog, search / Not visible individually) $product->setTaxClassId(0); // Tax class id $product->setTypeId('configurable'); // type of product (simple/virtual/downloadable/configurable) $product->setPrice(100); // price of product $product->setStockData(array('use_config_manage_stock' => 0, 'manage_stock' => 1, 'is_in_stock' => 1, 'qty' => 999999999)); $product->save(); } } |
Easy, ain’t it?
Creating Magento 2 configurable product is one of the basics so if you have any doubts on the topic, please mention them in the Comments section below. I’d be happy to help.
Do share the post with fellow Magento beginners via social media.
Thanks.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
4 Comments
How to assign a simple product to an existing configurable product?
Hello Sagar,
The above code is only for creating a new configurable product.
Thank You.
Hi Sanjay, do you have any example of php code, where we can assign multiple attributes such as color / size and then assign simple products to configurable product via code? Please help if possible.
Hello Prashant,
You’ll have to create the size and color attribute.
For example:
$attr = $_product->getResource()->getAttribute('color');
$avid = $attr->getSource()->getOptionId('Blue');
$_product->setData('color', $avid);
Thank you.