Magento: add configurable products to cart programmatically

Magento: add configurable products to cart programmatically

July 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: add configurable products to cart programmatically

Adding Configurable Products to Cart Programmatically in Magento

In Magento, adding a configurable product, which includes its child products with selected attributes, to a cart can be done programmatically using the Magento API. This involves fetching the required product information, preparing the selection data for the child product, and then adding it to the cart using the appropriate method.

Fetching Product Information

To begin, you need to fetch the necessary details of the configurable product and its associated child products. You can use the Magento_CatalogApi service to perform this operation:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productRepository = $objectManager->create('Magento\Catalog\Model\ProductFactory');

$childSku = 'CHILD-SKU-HERE';
$parentId = $childSku;

$product = $productRepository->create()->load($parentId);

In the above example, replace 'CHILD-SKU-HERE' with the actual SKU of the child product you want to add. The load() method fetches the product details from the Magento database using the provided product ID or SKU.

Preparing the Selection Data

Once you have loaded the parent configurable product, you can fetch its associated simple products and prepare the selection data:

$childProducts = $product->getTypeInstance()->bundleProductOptions($product);

foreach ($childProducts as $child) {
    if ($child['sku'] == $childSku) {
        // Found the child product with the specified SKU
        $option = [
            'info_buyRequest' => [
                'product' => $parentId,
                'sku' => $child['sku'],
                'qty' => 1, // Quantity of the item to add
                'options' => $child['selection_data']['option_id'] // Super attributes (size, color ID)
            ]
        ];

        break;
    }
}

In this code snippet, we loop through all the associated simple products of the configurable product. When a match is found with the specified child SKU, we prepare an array $option containing the selection data for that child product.

The info_buyRequest array includes the parent product ID ($parentId), the child product’s SKU, the quantity to add (in this case 1), and the super attributes required by the child product.

Adding the Product to Cart

With the selection data prepared, you can now use the Magento cart management API to add the child product to the customer’s cart:

$cart = $objectManager->create('Magento\Checkout\Model\Cart');
$cart->addProduct($parentId, $option);

The addProduct() method takes two parameters: the parent product ID and an array of options containing the selection data for the child product.

Tips for Programmatic Cart Operations

  1. Use ObjectManager Cautiously: The use of \Magento\Framework\App\ObjectManager::getInstance() is not recommended in a production environment due to performance implications. It’s better to inject the required dependencies through appropriate Magento services and classes.

  2. Checkout API: If you need to perform more complex operations like applying discounts or using custom prices, consider using the Magento_CheckoutApi service instead of the Cart model directly.

  3. Session Management: When working with carts programmatically, ensure that the customer’s session is properly managed and that any changes are saved correctly.

  4. Error Handling: Always include proper exception handling to manage potential errors during these operations, such as when a product is out of stock or has insufficient quantity.

Conclusion

Adding configurable products to the cart programmatically in Magento involves several steps: fetching the product details, preparing the selection data for the child product, and finally adding it to the customer’s cart. By following the methods demonstrated above, developers can easily integrate this functionality into their custom extensions or scripts.

Remember that the code examples provided here are simplified snippets. In a real-world scenario, you would need to handle dependencies, sessions, and other potential issues appropriately. Always refer to the official Magento API documentation for the most accurate and up-to-date information: https://adobe-commerce.redoc.ly/2.4.6-admin/tag/catalogProductManagementV1#operation/catalogCatalogproductAttributegetList

By mastering these techniques, you can create powerful Magento extensions that provide seamless and efficient customer experiences through the cart functionality.

Checkout Catalog Database API

Our network