Magento: add product twice to cart, with different attributes!

Magento: add product twice to cart, with different attributes!

August 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: add product twice to cart, with different attributes!

Solving the Magento Dual Attribute Product Cart Issue

Introduction

In this post, we’ll tackle an interesting challenge faced by many Magento developers - adding the same product to a cart with different attribute sets for each eye (left/right). This is common in cases like contact lenses where users might need different prescriptions for each eye. Let’s dive into the issue and find a practical solution.

The Problem

The developer, Peter, has encountered an unusual situation where he manages to add two products to the cart, but they both end up having identical attributes despite selecting different options per eye during checkout. This is due to Magento’s default behavior when adding products with custom options.

The Current Approach and Limitations

Peter has attempted two methods:

  1. Using addProduct in a loop with different attribute values:
$quote->addProduct($founditem->getProduct(), $reqo);

This approach aims to add the product twice with distinct options, but unfortunately, Magento merges these into a single item.

  1. Clearing and adding products manually using the session cart:
$cart = Mage::getSingleton('checkout/cart');
$cart->clear()->save();
$cart->addProduct($founditem->getProduct(), $reqo)->save();

While this method successfully clears the cart, it still fails to retain different attribute sets when multiple products are added.

The Solution: Utilizing Magento’s Custom Cart Item

Step 1: Create a custom quote item

To overcome this limitation, we need to create a custom quote item that can store separate options for each eye. This involves extending the Mage_Sales_Model_Quote_Item class and overriding its behavior.

Create a new file named CustomQuoteItem.php in your module:

class Clarus_CustomCartBlock_Model_Qitem extends Mage_Sales_Model_Quote_Item {
    protected $_options = array();

    public function setOptions($options) {
        $this->_options = $options;
        return $this;
    }

    public function getOptions() {
        return $this->_options;
    }
}

Step 2: Configure the custom item

Update your module’s configuration file (config.xml) to tell Magento to use our new quote item:

<global>
    <models>
        <clarus_customcartblock>
            <class>Clarus_CustomCartBlock_Model</class>
        </clarus_customcartblock>
    </models>
    <sales>
        <quote_item>
            <rewrite>
                <customitem>Clarus_CustomCartBlock_Model_Qitem</customitem>
            </rewrite>
        </quote_item>
    </sales>
</global>

Step 3: Update the cart addition logic

Now, let’s update Peter’s original code to use our custom quote item:

$cart = Mage::getSingleton('checkout/cart');
$cart->clear()->save();

$reqo1 = new Varien_Object($request);
$reqo1->setOptions(array(1 => 2, 3 => 6, 2 => 4));

$item1 = $cart->addProduct($founditem->getProduct(), $reqo1)->getItemByRowId(Mage::helper('core')->uniqHash());
$item1->setOptions($reqo1->getOptions());

$reqo2 = new Varien_Object($request);
$reqo2->setOptions(array(1 => 1, 3 => 5, 2 => 3));

$item2 = $cart->addProduct($founditem->getProduct(), $reqo2)->getItemByRowId(Mage::helper('core')->uniqHash());
$item2->setOptions($reqo2->getOptions());

$cart->save();

Step 4: Verify the changes

Now, when you add products to the cart with different options per eye, they should appear as separate items in the cart summary:

print_r($cart->getItems());

Conclusion

The key to resolving this issue lies in Magento’s custom quote item functionality. By creating a custom quote item and storing separate options for each eye, we can successfully add the same product twice with distinct attribute sets.

Remember, when dealing with complex cart scenarios like this one, it’s crucial to understand Magento’s core behavior and utilize its extensibility features effectively. This approach allows us to maintain the platform’s built-in functionality while adding custom logic tailored to our specific needs.

For more advanced Magento development techniques and real-world use cases, be sure to check out our comprehensive guide on Magento Customization.

Happy coding!

Checkout Catalog API Errors

Our network