{"id":840,"date":"2020-02-23T09:42:27","date_gmt":"2020-02-23T09:42:27","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/02\/23\/programmatically-create-configurable-product-in-magento-2\/"},"modified":"2025-05-22T14:56:13","modified_gmt":"2025-05-22T09:26:13","slug":"programmatically-create-configurable-product-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/programmatically-create-configurable-product-in-magento-2\/","title":{"rendered":"Programmatically Create Configurable Product in Magento 2 Simplified"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">If you are here, then you are a beginner in Magento 2 world! Welcome. It isn\u2019t as hard as people say  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/experienceleague.adobe.com\/en\/docs\/commerce-admin\/catalog\/products\/types\/product-create-configurable\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 configurable product<\/a>&nbsp;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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are&nbsp;<a href=\"https:\/\/experienceleague.adobe.com\/en\/docs\/commerce-admin\/catalog\/products\/product-create\" target=\"_blank\" rel=\"noreferrer noopener\">six product types<\/a>&nbsp;in Magento 2:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Simple Product<\/li>\n\n\n\n<li>Configurable Product<\/li>\n\n\n\n<li>Grouped Product<\/li>\n\n\n\n<li>Virtual Product<\/li>\n\n\n\n<li>Bundle Product<\/li>\n\n\n\n<li>Downloadable Product<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Learning how to&nbsp;<em><strong>programmatically create configurable product in Magento 2<\/strong><\/em>&nbsp;makes it easy for the developers while creating an online store.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, apparel available in different sizes or colors is an example of a configurable product.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check the below code to become a pro in creating Magento 2 configurable products!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to programmatically create configurable product in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace [Vendor]\\[Module]\\Controller;\nuse Magento\\Framework\\App\\Action\\Action;\nuse Magento\\Framework\\App\\Action\\Context;\nuse Magento\\Catalog\\Model\\Product;\nclass Test extends Action\n{\n    protected $customerSession;\n    protected $resultPageFactory;\n    protected $redirectFactory;\n    protected $newProduct;\n    public function __construct(Context $context, Product $newProduct, \\Magento\\Framework\\ObjectManagerInterface $objectManager)\n    {\n        $this->newProduct = $newProduct;\n        parent::__construct($context);\n\n    }\n    public function execute()\n    {\n        $product = $this->newProduct;\n        $product->setSku('mySku'); \/\/ Set your sku here\n        $product->setName('Sample Configurable Product'); \/\/ Name of Product\n        $product->setAttributeSetId(4); \/\/ Attribute set id\n        $product->setStatus(1); \/\/ Status on product enabled\/ disabled 1\/0\n        $product->setWeight(10); \/\/ weight of product\n        $product->setVisibility(4); \/\/ visibilty of product (catalog \/ search \/ catalog, search \/ Not visible individually)\n        $product->setTaxClassId(0); \/\/ Tax class id\n        $product->setTypeId('configurable'); \/\/ type of product (simple\/virtual\/downloadable\/configurable)\n        $product->setPrice(100); \/\/ price of product\n        $product->setStockData(array('use_config_manage_stock' => 0, 'manage_stock' => 1, 'is_in_stock' => 1, 'qty' => 999999999));\n        $product->save();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it. Also in Magento 2 you can get&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/get-simple-product-id-from-configurable-product-in-magento-2\/\">simple product ID from configurable product<\/a>&nbsp;to get child product ID of a configurable product by swatch options using jQuery.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Easy, ain\u2019t it?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do share the post with fellow Magento beginners via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are here, then you are a beginner in Magento 2 world! Welcome. It isn\u2019t as hard as people say Magento 2 configurable product&nbsp;is&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-840","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/840","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=840"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/840\/revisions"}],"predecessor-version":[{"id":15072,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/840\/revisions\/15072"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}