How to Add Magento 2 Configurable Products Programmatically to Cart
A quick method for developers who get bored to check the functionalities they implement! (including me 😉)
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.
Hence, I came up with a solution that allowed me to add Magento 2 configurable products programmatically to cart which reduced my tedious task!
Sharing the solution for my readers,
Steps to Add Magento 2 Configurable Products Programmatically to Cart:
- Create registration.php file in app\code\[Vendor]\[Namespace]\
123456<?php\Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,'[Vendor]_[Namespace]',__DIR__); - Create module.xml file in app\code\[Vendor]\[Namespace]\etc
12345<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"><module name="[Vendor]_[Namespace]" setup_version="1.0.0"/></config> - Create Data.php file in app\code\[Vendor]\[Namespace]\Helper
12345678910111213141516171819202122232425262728293031323334353637383940414243444546<?phpnamespace [Vendor]\[namespace]\Helper;use Magento\Framework\App\Helper\AbstractHelper;use Magento\Framework\App\Helper\Context;use Magento\Checkout\Model\Cart;use Magento\Catalog\Model\ProductFactory;class Data extends AbstractHelper{private $cart;private $productFactory;public function __construct(Context $context, Cart $cart, ProductFactory $productFactory){$this->productFactory = $productFactory;$this->cart = $cart;parent::__construct($context);}public function getAddConfigurableProduct($parentId, $childId){$parent = $this->productFactory->load($parentId);$child = $this->productFactory->load($childId);$cart = $this->cart;$params = [];$params['product'] = $parent->getId();$params['qty'] = '1';$options = [];$productAttributeOptions = $parent->getTypeInstance(true)->getConfigurableAttributesAsArray($parent);foreach ($productAttributeOptions as $option) {$options[$option['attribute_id']] = $child->getData($option['attribute_code']);}$params['super_attribute'] = $options;/*Add product to cart */$cart->addProduct($parent, $params);$cart->save();}}
You can use the above helper method by passing parent product id and child product id in anywhere in Magento 2.
That’s it. After done with this you can also get Magento 2 simple product ID from configurable product to get child product ID of configurable product by swatch options using jQuery.
Whenever you are required to add configurable products to the cart while Magento 2 development, you may use this programmatic method!
Bookmark this post and save yourself from the tiresome task forever!
Hope it helps. You may use the Comments section below to mention the doubts on the topic.
Thank you!
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.
Prev
Our Solution For Magento Logging Failed After Installing SUPEE 11086 Patch
Magento 2.3.2: Everything you need to know about the Latest Release
Next