{"id":478,"date":"2019-06-25T12:20:27","date_gmt":"2019-06-25T12:20:27","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/06\/25\/add-magento-2-configurable-products-to-cart\/"},"modified":"2025-05-22T16:32:28","modified_gmt":"2025-05-22T11:02:28","slug":"add-magento-2-configurable-products-to-cart","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-magento-2-configurable-products-to-cart\/","title":{"rendered":"How to Add Magento 2 Configurable Products Programmatically to Cart"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A quick method for developers who get bored to check the functionalities they implement! (including me  )<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I was working on a module development task that required to check each and every feature with a simple product and configurable product. Each time repeating the process to select the options, add to cart, and checkout was not feasible.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hence, I came up with a solution that allowed me to&nbsp;<em><strong>add Magento 2 configurable products programmatically to cart<\/strong><\/em>&nbsp;which reduced my tedious task!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sharing the solution for my readers,<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Add Magento 2 Configurable Products Programmatically to Cart:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong>registration.php<\/strong>&nbsp;file in&nbsp;<em><em><strong><em><em><strong>app\\code\\[Vendor]\\[Namespace]\\<\/strong><\/em><\/em><\/strong><\/em><\/em><\/p>\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\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\n    \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n    '[Vendor]_[Namespace]',\n    __DIR__\n);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>module.xml<\/strong>&nbsp;file in&nbsp;<em><em><strong><em><em><strong>app\\code\\[Vendor]\\[Namespace]\\etc<\/strong><\/em><\/em><\/strong><\/em><\/em><\/p>\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;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n    &lt;module name=\"[Vendor]_[Namespace]\" setup_version=\"1.0.0\"\/>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create&nbsp;<strong>Data.php<\/strong>&nbsp;file in&nbsp;<em><em><strong><em><em><strong>app\\code\\[Vendor]\\[Namespace]\\Helper<\/strong><\/em><\/em><\/strong><\/em><\/em><\/p>\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]\\[namespace]\\Helper;\n\nuse Magento\\Framework\\App\\Helper\\AbstractHelper;\nuse Magento\\Framework\\App\\Helper\\Context;\nuse Magento\\Checkout\\Model\\Cart;\nuse Magento\\Catalog\\Model\\ProductFactory;\n\nclass Data extends AbstractHelper\n{\n    private $cart;\n    private $productFactory;\n    public function __construct(Context $context, Cart $cart, ProductFactory $productFactory)\n    {\n\n        $this->productFactory = $productFactory;\n        $this->cart = $cart;\n        parent::__construct($context);\n\n    }\n\n    public function getAddConfigurableProduct($parentId, $childId)\n    {\n        $parent = $this->productFactory->load($parentId);\n        $child = $this->productFactory->load($childId);\n\n        $cart = $this->cart;\n\n        $params = [];\n        $params['product'] = $parent->getId();\n        $params['qty'] = '1';\n        $options = [];\n\n        $productAttributeOptions = $parent->getTypeInstance(true)->getConfigurableAttributesAsArray($parent);\n\n        foreach ($productAttributeOptions as $option) {\n            $options[$option['attribute_id']] = $child->getData($option['attribute_code']);\n        }\n        $params['super_attribute'] = $options;\n\n        \/*Add product to cart *\/\n        $cart->addProduct($parent, $params);\n        $cart->save();\n\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can use the above helper method by passing parent product id and child product id in anywhere in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it. After done with this you can also&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/get-simple-product-id-from-configurable-product-in-magento-2\/\">get Magento 2 simple product ID from configurable product<\/a>&nbsp;to get child product ID of configurable product by swatch options using jQuery.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Whenever you are required to add configurable products to the cart while Magento 2 development, you may use this programmatic method!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Bookmark this post and save yourself from the tiresome task forever!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A quick method for developers who get bored to check the functionalities they implement! (including me ) I was working on a module development task&#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-478","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/478","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=478"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/478\/revisions"}],"predecessor-version":[{"id":15371,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/478\/revisions\/15371"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}