Item (Magento\Catalog\Model\Product\Interceptor) with the same “ID” ID already exists. A common source of this error is a duplicate entry in cataloginventory_stock_item
.
The mentioned ID was a product that I manually deleted. I checked it twice (in backend and database) – there is no product with this entity_id. I show you how to solve this error.
See this entry in Exception Log :
The best way to do that is by creating a Magento 2 plugin to extend the behavior of the public function AddItem of the class.
The plugin or Interceptor is a class the modifies the behavior of the public class method by interceptions. The plugin gives use three options to use After methods, Before methods and Around Method.
This issue occur if you delete a product in your backend and some information about its ID is not deleted. I had this problem in backend when I want to create a data feed of products. At first I was not able to find the problem, because a product with ID was not found and a direct connection to magento database confirms this. I supposed, that I deleted a wrong imported product a day before, but was not sure about its ID.
You can use the module below to resolve the above error.
Create registration.php file in the app/code/Dolphin/DuplicateEntry folder with the following code.
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Dolphin_DuplicateEntry', __DIR__ );
Create a module.xml file in the app/code/Dolphin/DuplicateEntry/etc folder with the following code.
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Dolphin_DuplicateEntry" setup_version="1.0.1"></module> </config>
Also like to Read: Add Product To Cart With Custom Price in Magento 2
Create a di.xml file in the app/code/Dolphin/DuplicateEntry/etc folder with the following code.
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Eav\Model\Entity\Collection\AbstractCollection"> <plugin name="duplicate_entry_already_exists_bug" type="Dolphin\DuplicateEntry\Plugin\Eav\Model\Entity\Collection" sortOrder="10"/> </type> </config>
Create a Collection.php file in the app/code/Dolphin/DuplicateEntry/Plugin/Eav/Model/Entity folder with the following code.
<?php namespace Dolphin\DuplicateEntry\Plugin\Eav\Model\Entity; use Magento\Framework\Data\Collection\EntityFactoryInterface; use Magento\Framework\Option\ArrayInterface; class Collection { /** * @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $subject * @param \Closure $process * @param \Magento\Framework\DataObject $dataObject * @return $this */ public function aroundAddItem(\Magento\Eav\Model\Entity\Collection\AbstractCollection $subject, \Closure $process, \Magento\Framework\DataObject $dataObject) { try{ return $process($dataObject); }catch ( \Exception $e){ return $this; } } }
callable
, it will prevent the execution of all the plugins next in the chain and the original method call.[/dt_highlight]If you are facing any issues during implementation, use the comment section below!
If you are looking for a Magento developer to fulfill your Magento 2 development needs, choose Dolphin Web Solution.
Happy Coding!