---
title: "Magento 2: How to Programmatically Add Configurable Products to Cart"
url: "https://magecomp.com/blog/magento-2-programmatically-add-configurable-products-to-cart/"
date: "2024-08-09T12:30:38+00:00"
modified: "2025-02-12T10:41:20+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 379
reading_time: "2 min read"
summary: "Hello Magento Friends,"
description: "Learn how to programmatically add configurable products to the cart in Magento 2. Perfect for developers building custom modules or integrations."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Use Tab Widget in Magento 2"
    url: "https://magecomp.com/blog/use-tab-widget-in-magento-2/"
  - title: "How to Get Associated Product Information from Grouped Product SKU in Magento 2"
    url: "https://magecomp.com/blog/get-associated-product-information-from-grouped-product-sku-magento-2/"
  - title: "How to Get System Configuration Value using GraphQL API in Magento 2"
    url: "https://magecomp.com/blog/get-system-configuration-value-using-graphql-api-magento-2/"
---

# Magento 2: How to Programmatically Add Configurable Products to Cart

_Published: August 9, 2024_  
_Author: Dhiren Vasoya_  

![Magento 2 How to Programmatically Add Configurable Products to Cart](https://magecomp.com/blog/wp-content/uploads/2024/08/Magento-2-How-to-Programmatically-Add-Configurable-Products-to-Cart-1024x512.webp)

**Hello Magento Friends,**

In today’s blog, we will learn about How to Programmatically Add Configurable Products to Cart in Magento 2.
[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-4.webp)](https://magecomp.com/services/hyva-theme-development/)

Adding products to the cart programmatically is a common requirement when building custom functionalities in Magento 2. This is especially important when dealing with configurable products, as these require not only the main product information but also the associated child product (simple product) based on selected attributes like size, color, etc.
> [**Magento 2: Get Simple Product from Configurable Product before Add to Cart**](https://magecomp.com/blog/magento-2-get-simple-product-from-configurable-product-before-add-to-cart/)

In this blog, we’ll guide you through the process of programmatically adding configurable products to the cart in Magento 2.
## Steps to Programmatically Add Configurable Products to Cart in Magento 2:
**Step 1:** We need to create a **“configurable_addtocart.php“** file inside the Magento root directory.
**magento_root_directory\configurable_addtocart.php**

Then add the code as follows
```
<?php
	use MagentoFrameworkAppInterface;

	try {
	    require_once __DIR__ . '/app/bootstrap.php';

	} catch (Exception $e) {
	    echo 'Autoload error: ' . $e->getMessage();
	    exit(1);
	}
	try{
		$bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
		$objectManager = $bootstrap->getObjectManager();
		$appState = $objectManager->get('MagentoFrameworkAppState');
		$appState->setAreaCode('frontend');

		$objectManager = MagentoFrameworkAppObjectManager::getInstance();
		$cart = $objectManager->get('MagentoCheckoutModelCart');
		$productId = 10; // set Configurable Product Id
		$product = $objectManager->create('MagentoCatalogModelProductFactory')->create()->load($productId);
		$_children = $product->getTypeInstance()->getUsedProducts($product);
	    	foreach ($_children as $child){
		$childId = $child->getID();
			$childProduct = $objectManager->create('MagentoCatalogModelProductFactory')->create()->load($childId);
			$params = [];
			$params['product'] = $product->getId();
			$params['qty'] = 2;
			$options = [];
			$productAttributeOptions = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
			foreach($productAttributeOptions as $option){
			    $options[$option['attribute_id']] =  $childProduct->getData($option['attribute_code']);
			}
			$params['super_attribute'] = $options;

			/*Add product to cart */
			$product = $objectManager->create('MagentoCatalogModelProductFactory')->create()->load($product->getId());
			$cart->addProduct($product, $params);
			$cart->save();
	    	}
		echo "Product Added Successfully";
	}
	catch(Exception $e){
		print_r($e->getMessage());
	}
?>
```

## [![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-2.webp)](https://magecomp.com/services/magento-upgrade-services/)

## Conclusion:
By following the above step, you can effectively add configurable products to the cart programmatically in your Magento 2 store. If you have any doubts, let me know through the comment box. Share the tutorial with your friends and stay tuned with us for more.
[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-3.webp)](https://magecomp.com/magento-performance-optimization.html)

**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/magento-2-programmatically-add-configurable-products-to-cart/](https://magecomp.com/blog/magento-2-programmatically-add-configurable-products-to-cart/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-16 22:39:03 UTC_  
