1

I am trying to debug this error which happens whenever a logged in customer adds a coupon code during checkout.

a:5:{i:0;s:67:"Item (Mage_SalesRule_Model_Rule) with the same id "5" already exist";i:1;s:1977:"#0 /var/www/vhosts/test.devcom/httpdocs/lib/Varien/Data/Collection/Db.php(576): Varien_Data_Collection->addItem(Object(Mage_SalesRule_Model_Rule))
#1 /var/www/vhosts/test.dev.com/httpdocs/app/code/core/Mage/SalesRule/Model/Validator.php(117): Varien_Data_Collection_Db->load()
#2 /var/www/vhosts/test.dev.com/httpdocs/app/code/core/Mage/SalesRule/Model/Quote/Freeshipping.php(60): Mage_SalesRule_Model_Validator->init('1', '1', 'INSTA20')
#3 /var/www/vhosts/test.dev.com/httpdocs/app/code/core/Mage/Sales/Model/Quote/Address.php(1013): Mage_SalesRule_Model_Quote_Freeshipping->collect(Object(Mage_Sales_Model_Quote_Address))
#4 /var/www/vhosts/test.dev.com/httpdocs/app/code/core/Mage/Sales/Model/Quote.php(1331): Mage_Sales_Model_Quote_Address->collectTotals()
#5 /var/www/vhosts/test.dev.com/httpdocs/app/code/core/Mage/Checkout/Model/Cart.php(458): Mage_Sales_Model_Quote->collectTotals()
#6 /var/www/vhosts/test.dev.com/httpdocs/app/code/core/Mage/Checkout/controllers/CartController.php(127): Mage_Checkout_Model_Cart->save()
#7 /var/www/vhosts/test.dev.com/httpdocs/app/code/core/Mage/Core/Controller/Varien/Action.php(418): Mage_Checkout_CartController->indexAction()
#8 /var/www/vhosts/test.dev.com/httpdocs/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('index')
#9 /var/www/vhosts/test.dev.com/httpdocs/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#10 /var/www/vhosts/test.dev.com/httpdocs/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#11 /var/www/vhosts/test.dev.com/httpdocs/app/Mage.php(683): Mage_Core_Model_App->run(Array)
#12 /var/www/vhosts/test.dev.com/httpdocs/index.php(87): Mage::run('', 'store')
#13 {main}";s:3:"url";s:15:"/checkout/cart/";s:11:"script_name";s:10:"/index.php";s:4:"skin";s:7:"default";}

I'm not sure what is causing the error since the coupon code works when checking out as a guest.

Has anyone had this error before and know what the cause could be? I'd appreciate any assistance please...

4

Please do take a backup of code:

Go to file

lib/Varien/Data/Collection.php

Modify this method

public function addItem

With the following code

public function addItem(Varien_Object $item)
    {
        $itemId = $this->_getItemId($item);
        if (!is_null($itemId)) {
            if (isset($this->_items[$itemId])) {
              //  throw new Exception('Item ('.get_class($item).') with the same id "'.$item->getId().'" already exist');
            } else {
                $this->_items[$itemId] = $item;
            }
        }
        else {
            $this->_items[] = $item;
        }
        return $this;
    } 
  • I can see that this does solve the error but what would have caused the exception to be thrown with any core files being modified? I will accept your as has stopped the error but I would also like to know what the cause is/was... – user1704524 May 9 '14 at 12:13
  • I think, When you load several products in one page like two product list or when you call many products through module (new, specials, etc.) and two products are identical, magento generate an error like this. – TBI Infotech May 9 '14 at 12:17
  • Also, it is better to move lib/Varien/Data/Collection.php to app/code/local/Varien/Data/Collection.php – user1704524 May 11 '14 at 9:20
  • Worst Solution Ever. Modifying a so "low level" object, used by every collection in Magento, to introduce a new bug. WTF Guys. The true pb comes from old version of Magento, in Mage_SalesRule_Model_Resource_Rule_Collection in the function setValidationFilter when you have a automated coupon code coupon. Watch last version of Magento, like github.com/OpenMage/magento-lts/blob/1.9.3.x/app/code/core/Mage/… to see the fix – Pol Ravalitera Aug 30 '18 at 13:53
-1

you can also override your collection calss Mage_Sales_Model_Resource_Order_Grid_Collection::addItem which throws this exeption :

<?php
class Fixed_Order_Grid_Collection extends Mage_Sales_Model_Resource_Order_Grid_Collection{

    public function addItem(Varien_Object $item)
    {
        $itemId = $this->_getItemId($item);

        if (!is_null($itemId)) {
            if (isset($this->_items[$itemId])) {
                //throw new Exception('Item ('.get_class($item).') with the same id "'.$item->getId().'" already exist');
            }
            $this->_items[$itemId] = $item;
        } else {
            $this->_addItem($item);
        }
        return $this;
    }
}
class MyCompony_MyExtention_Block_Adminhtml_OrderGrid extends Mage_Adminhtml_Block_Widget_Grid
{
     public function __construct()
    {
        parent::__construct();
        $this->setId('sales_order_grid');
        $this->setUseAjax(true);
        $this->setDefaultSort('created_at');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
    }
    protected function _getCollectionClass()
    {
        return 'sales/order_grid_collection';
    }

    protected function _prepareCollection()
    {
        $collection = new Fixed_Order_Grid_Collection();
        $select = $collection->getSelect();
        $select->join('sales_flat_order_item AS order_item', 'order_item.order_id=main_table.entity_id','quote_item_id',NULL);
        $select->distinct();
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.