Magento Reports - Products - Products Ordered Problem: Product (Mage_Catalog_Model_Product) with the same identifier already exists

Problem: In the Magento admin panel, when creating a report through Reports - Products - Products Ordered, an error occurs:

     Item (Mage_Catalog_Model_Product) with the same id "45" already exist
     # 0 /home/glassesm/public_html/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php(236): Varien_Data_Collection-> addItem (Object (Mage_Catalog_Model_Product))
     # 1 /home/glassesm/public_html/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php(964): Mage_Eav_Model_Entity_Collection_Abstract-> addItem (Object (Mage_Catalog_Model_Product))
     # 2 /home/glassesm/public_html/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php(794): Mage_Eav_Model_Entity_Collection_Abstract -> _ loadEntities (false, false)
     # 3 /home/glassesm/public_html/lib/Varien/Data/Collection.php(729): Mage_Eav_Model_Entity_Collection_Abstract-> load ()
     # 4 /home/glassesm/public_html/app/code/core/Mage/Reports/Model/Totals.php(53): Varien_Data_Collection-> getIterator ()
     # 5 /home/glassesm/public_html/app/code/core/Mage/Adminhtml/Block/Report/Grid.php(368): Mage_Reports_Model_Totals-> countTotals (Object (Mage_Adminhtml_Block_Report_Product_Sold_Grid), 0-12, 2011. .. ',' 2011-12-10 23: 5 ... ')
     # 6 /home/glassesm/public_html/app/design/adminhtml/default/default/template/report/grid.phtml(138): Mage_Adminhtml_Block_Report_Grid-> getReport ('2011-12-10 00: 0 ...', ' 2011-12-10 23: 5 ... ')
     # 7 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Template.php(216): include ('/ home / glassesm / ...')
     # 8 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Template.php(247): Mage_Core_Block_Template-> fetchView ('adminhtml / defau ...')
     # 9 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Template.php(261): Mage_Core_Block_Template-> renderView ()
     # 10 /home/glassesm/public_html/app/code/core/Mage/Adminhtml/Block/Template.php(81): Mage_Core_Block_Template -> _ toHtml ()
     # 11 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Abstract.php(758): Mage_Adminhtml_Block_Template -> _ toHtml ()
     # 12 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Abstract.php(525): Mage_Core_Block_Abstract-> toHtml ()
     # 13 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Abstract.php(476): Mage_Core_Block_Abstract -> _ getChildHtml ('grid', true)
     # 14 /home/glassesm/public_html/app/code/core/Mage/Adminhtml/Block/Widget/Grid/Container.php(70): Mage_Core_Block_Abstract-> getChildHtml ('grid')
     # 15 /home/glassesm/public_html/app/design/adminhtml/default/default/template/widget/grid/container.phtml(36): Mage_Adminhtml_Block_Widget_Grid_Container-> getGridHtml ()
     # 16 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Template.php(216): include ('/ home / glassesm / ...')
     # 17 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Template.php(247): Mage_Core_Block_Template-> fetchView ('adminhtml / defau ...')
     # 18 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Template.php(261): Mage_Core_Block_Template-> renderView ()
     # 19 /home/glassesm/public_html/app/code/core/Mage/Adminhtml/Block/Template.php(81): Mage_Core_Block_Template -> _ toHtml ()
     # 20 /home/glassesm/public_html/app/code/core/Mage/Adminhtml/Block/Widget/Container.php(295): Mage_Adminhtml_Block_Template -> _ toHtml ()
     # 21 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Abstract.php(758): Mage_Adminhtml_Block_Widget_Container -> _ toHtml ()
     # 22 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Text/List.php(43): Mage_Core_Block_Abstract-> toHtml ()
     # 23 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Abstract.php(758): Mage_Core_Block_Text_List -> _ toHtml ()
     # 24 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Abstract.php(525): Mage_Core_Block_Abstract-> toHtml ()
     # 25 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Abstract.php(476): Mage_Core_Block_Abstract -> _ getChildHtml ('content', true)
     # 26 /home/glassesm/public_html/app/design/adminhtml/default/default/template/page.phtml(74): Mage_Core_Block_Abstract-> getChildHtml ('content')
     # 27 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Template.php(216): include ('/ home / glassesm / ...')
     # 28 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Template.php(247): Mage_Core_Block_Template-> fetchView ('adminhtml / defau ...')
     # 29 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Template.php(261): Mage_Core_Block_Template-> renderView ()
     # 30 /home/glassesm/public_html/app/code/core/Mage/Adminhtml/Block/Template.php(81): Mage_Core_Block_Template -> _ toHtml ()
     # 31 /home/glassesm/public_html/app/code/core/Mage/Core/Block/Abstract.php(758): Mage_Adminhtml_Block_Template -> _ toHtml ()
     # 32 /home/glassesm/public_html/app/code/core/Mage/Core/Model/Layout.php(529): Mage_Core_Block_Abstract-> toHtml ()
     # 33 /home/glassesm/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(389): Mage_Core_Model_Layout-> getOutput ()
     # 34 /home/glassesm/public_html/app/code/core/Mage/Adminhtml/controllers/Report/ProductController.php(97): Mage_Core_Controller_Varien_Action-> renderLayout ()
     # 35 /home/glassesm/public_html/app/code/core/Mage/Core/Controller/Varien/Action.php(418): Mage_Adminhtml_Report_ProductController-> soldAction ()
     # 36 /home/glassesm/public_html/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(253): Mage_Core_Controller_Varien_Action-> dispatch ('sold')
     # 37 /home/glassesm/public_html/app/code/core/Mage/Core/Controller/Varien/Front.php(176): Mage_Core_Controller_Varien_Router_Standard-> match (Object (Mage_Core_Controller_Request_Http))
     # 38 /home/glassesm/public_html/app/code/core/Mage/Core/Model/App.php(340): Mage_Core_Controller_Varien_Front-> dispatch ()
     # 39 /home/glassesm/public_html/app/Mage.php(627): Mage_Core_Model_App-> run (Array)
     # 40 /home/glassesm/public_html/index.php(80): Mage :: run ('', 'store')
     # 41 {main}

Could you help me with this problem?

+4
source share
6 answers

In fact, I have 2 different stores on two different servers, and both with magento 1.6. I have this problem in both stores. The database is checked, and there are no two entries.

My solution was:

Edit: /www/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php
Line: 256

Replace: return parent::addItem($object);

C: try { return parent::addItem($object); } catch (Exception $ex) { } try { return parent::addItem($object); } catch (Exception $ex) { }

It works great, but any other solution is welcome.

+5
source

this is because your product collection returns duplicate product identifiers that you can group your product collection by grouping "e.entity_id"

try it

 $collection = Mage::getModel('catalog/product') ->getCollection() ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left') ->addAttributeToSelect('*') ->addAttributeToFilter('category_id', array('in' => $uniqueIds)); <?php $collection->getSelect()->group('e.entity_id');?> 
+4
source

This means that you have two products with the same identifier. This probably happened when you were working with importing or mass updating your products.

A simple solution is to find the product with identifier 35 and either delete it because it is a duplicate, or change the identifier because it must be unique. Please note that you should search all related tables, not just the product table. The problem may be with your EAV attribute table or with any number of others.

+1
source

It turns out the same error in the same place, that's why and how I fixed it.

Magento order fulfillment reports build a "list" from "sales_flat_order_item", then join the "catalog / product" for more product information (sku, type, options, attribute_set, etc.).

In my case, 3 products were removed from the store, when Magento built the list and joined the catalog / product, all the information above was NULL. When the Report module creates a collection, in the addItem method it tries to set the identifier as the key in the collection array, there is no identifier in this remote product, therefore NULL is set.

Imagine this set of elements for creating a report.

ID - Name: 1, 2, 3, REMOTE ITEM, 4

When "DELETE ITEM" is added to the collection, it will look like this:

 Array( 1 => Object, 2 => Object, 3 => Object, 4 => Object (DELETED ITEM) ) 

This is due to the fact that in addItem, when there is no Magento ID, add a new position to the array with automatic addition:

 if (!is_null($itemId)) { . . . } else { $this->_items[] = $item; } 

Then, when the collection adds the following element (ID: 4), you will get the exception that you are looking at:

 if (isset($this->_items[$itemId])) { throw new Exception('Item ('.get_class($item).') with the same id "'.$item->getId().'" already exist'); } 

Changing the way addItem works is very dangerous! Collections are used throughout Magento by adding a trycatch block that will definitely affect how your store works in other places / modules. Here is how I fixed it:

You can either copy to local or ovewrite using a custom module, it is up to you:

Add a new method to "Reports_Product / Collection" to filter only "existing products"

Application / code / local / Mage / Reports / Model / mysql4 / product / Collection.php

 public function addNotDeletedProductRestrition(){ $this->getSelect()->where('order_items.product_id is NOT NULL'); return $this; } 

Call this method in the ORDERED collection.

Application / code / local / Mage / Reports / Model / mysql4 / Product / Ordered / Collection.php

 protected function _joinFields($from = '', $to = '') { $this->addAttributeToSelect('*') ->addOrderedQty($from, $to) ->setOrder('ordered_qty', 'desc') ->addNotDeletedProductRestrition(); return $this; } 

I placed inside _joinFields, but you can call wherever you want.

Sorry for my rusty English, I do not have much time to review, this error has already accepted me a lot.

0
source

you can also override your calss Mage_Sales_Model_Resource_Order_Grid_Collection::addItem that raise this exception:

 <?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(); } ... 
-2
source

Just go to the admin panel, open the product (the first line shows the ID) and save it. Do this in all store views. Now the problem should disappear.

-6
source

Source: https://habr.com/ru/post/1387922/


All Articles