magento add extra product to cart automatically

magento add extra product to cart automatically

September 30, 2026 · Illustrated author avatarBy Stefan Bogdanescu
magento add extra product to cart automatically

Automatically Add Products to Cart in Magento: A Comprehensive Guide

Adding products to the cart programmatically is a common requirement in e-commerce development. In this tutorial, we’ll guide you through adding extra components automatically to your customer’s cart when they add a specific product.

Understanding the Requirement

You want to add assembly parts (extra components) to the cart whenever your main product (Product Y) is added. The key here is that these additional products should have the same quantity as Product Y in the cart.

Using Magento 2 Observers for Automatic Cart Addition

Magento provides built-in event observers which can be utilized to trigger actions at specific points during the checkout process. For our scenario, we’ll use the checkout_cart_add_product event.

Creating an Observer

  1. Create a new observer file under Vendor/Module/etc/events.xml:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:magento:module:url_etc/events.xsd">
    <event name="checkout_cart_add_product">
        <observer name="add_assembly_components_to_cart"
                  instance="Vendor\Module\Observer\AddAssemblyComponentsToCart"/>
    </event>
</config>
  1. Create the observer class AddAssemblyComponentsToCart under Vendor/Module/Observer:
namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Model\Quote\Item;

class AddAssemblyComponentsToCart implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        /** @var Item $item */
        $item = $observer->getEvent()->getItem();

        if ($item->getParentId() === null && $item->getSku() == 'YOUR_PRODUCT_SKU') {
            // Add your assembly components here
            $assemblyComponent1 = ['sku' => 'ASSEMBLY_PART_1_SKU', 'qty' => 1];
            $assemblyComponent2 = ['sku' => 'ASSEMBLY_PART_2_SKU', 'qty' => 1];

            foreach ($assemblyComponents as $component) {
                $item->getQuote()->addProduct($component['sku'], $component['qty']);
            }
        }

        return $this;
    }
}

Modifying Your Checkout Cart

When a customer adds Product Y to the cart, this observer triggers and automatically adds your assembly components with the same quantity as Product Y.

Conclusion

Adding products to the cart programmatically in Magento is achieved through event observers. By using the checkout_cart_add_product event, you can add extra components whenever a specific product (Product Y) is added to the cart.

Remember to replace 'YOUR_PRODUCT_SKU' and the assembly component SKUs with your actual product IDs. You may also need to adjust the quantity logic based on your exact requirements.

For more advanced customizations and best practices in Magento development, I recommend exploring our comprehensive guides at Laravel Company. We cover everything from theme creation to complex module development.

Happy coding!

Magento 2 Checkout Catalog Extensions

Our network