Magento Cart Same product adding in different lines

Magento Cart Same product adding in different lines

September 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Cart Same product adding in different lines

Managing Quantities of Identical Products in Magento Cart

As an e-commerce store owner or developer, you might face the common issue where identical products are added to the cart as separate lines instead of updating the existing quantity. This can lead to customer confusion and potentially lost revenue due to incorrect stock management. In this post, we’ll explore how to solve this problem by merging identical items into a single line with updated quantities.

Understanding the Problem

By default in Magento 2, when you add the same product to the cart multiple times, it creates separate lines for each quantity added. For example:

Initial Cart:

  • Product A: Quantity = 1
  • Product B: Quantity = 0

After Adding:

  • Product A: Quantity = 3 (separate line)
  • Product C: Quantity = 2
  • Product D: Quantity = 1

As you can see, adding three more of the same product A results in a new separate line instead of updating the existing quantity. This is not desirable from a user experience perspective.

Solution Approach

To solve this issue, we need to override Magento’s default cart behavior and implement custom functionality that checks for identical products before adding them to the cart. We can use the Magento\Quote\Model\Quote class to manage the cart items.

Here’s a high-level overview of the steps involved:

  1. Create an observer for the event sales_quote_add_item.
  2. In the observer, retrieve the quote model and find the item by product ID.
  3. If the item exists, update its quantity; otherwise, add it as a new line.
  4. Save the updated quote.

Implementing the Solution

Let’s dive into the implementation details:

Step 1: Create an Observer for sales_quote_add_item Event

First, you need to create an observer class that listens for the sales_quote_add_item event. You can use Magento’s plugin architecture or observer pattern.

Observer Class Example:

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

class MergeIdenticalItems implements ObserverInterface
{
    private $quoteFactory;

    public function __construct(
        QuoteFactory $quoteFactory
    ) {
        $this->quoteFactory = $quoteFactory;
    }

    public function execute(Observer $observer)
    {
        // Retrieve the quote model from the observer data
        $quoteId = $observer->getEvent()->getQuote()->getId();
        $productId = $observer->getEvent()->getItem()->getProduct()->getId();

        // Get the quote item by product ID
        $quote = $this->quoteFactory->create()->load($quoteId);
        $itemCollection = $quote->getItemsCollection()
            ->addFieldToFilter('product_id', $productId);

        if ($itemCollection->getSize() > 0) {
            // Item already exists in the cart, update its quantity
            $existingItem = $itemCollection->getFirstItem();
            $existingItem->setQty($existingItem->getQty() + $observer->getEvent()->getItem()->getQty());
        } else {
            // Add the item as a new line if it doesn't exist
            $quote->addItem($observer->getEvent()->getItem());
        }

        $quote->save();
    }
}

Step 2: Configure the Observer in Your Config File

To ensure that your observer is triggered when adding items to the cart, you need to configure it in your module’s etc/events.xml file:

events.xml Example:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_quote_add_item">
        <observer name="merge_identical_items" instance="Vendor\ModuleName\Model\Observer\MergeIdenticalItems" />
    </event>
</config>

Conclusion

In this post, we explored a common challenge faced by Magento developers when managing identical products in the cart. By implementing a custom observer that checks for existing items and updates their quantities, you can provide a seamless user experience while ensuring accurate stock management.

Remember to replace Vendor\ModuleName with your actual module name in the example code above.

By following this approach, you can create a more intuitive shopping experience for your customers while maintaining precise control over your inventory.

Magento 2 Checkout Catalog Extensions

Our network