Magento

How to Fix Same ID already exists error in Magento 2

Written by Jigar Patel

Sep 27

How to Fix Same ID already exists error in Magento 2

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 :

same id

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.

How to Fix Same ID already exists error in Magento 2

You can use the module below to resolve the above error.

Step 1: Create Registration

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__
);

Step 2: Create a module

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

Step 3: Create Dependency injection

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>

Step 4: Create plugin

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;
        }
    }
}
  • aroundAddItem: around methods before and after their observed methods.Using these methods allow you to override an observed method. around methods must have the same name as the observed method with ‘around’ as the prefix.[dt_highlight color=”” text_color=”” bg_color=””]If the around method does not call the 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!

Happy Coding!

Written by Jigar Patel

Our Magento Store

Do not miss a chance to grab exciting offers on Magento Development at Dolphin Web Solution. We are providing discounts on various Magento services this season. Go and grab yours today at our Magento Store.

Multiple Wishlist for Magento 2

Multiple Wishlist for Magento 2

₹ 3106

Wallet and Reward Points for Magento 2

Wallet and Reward Points for Magento 2

₹ 9476

RMA for Magento 2

RMA for Magento 2

₹ 11865

₹ 14254
Abandoned Cart Email for Magento 2

Abandoned Cart Email for Magento 2

₹ 6291

Simple Details on Configurable Product for Magento 2

Simple Details on Configurable Product for Magento 2

₹ 7883

₹ 9476
Frequently Bought Together for Magento 2

Frequently Bought Together for Magento 2

₹ 5494

₹ 7087

Let's Start Your Project

Get free consultation for your digital product idea to turn it into reality!

Copyright © 2023 DOLPHIN WEB SOLUTION. All rights reserved.

TO TOP