How to Fix Same ID already exists error in Magento 2

Written by Jigar Patel

Sep 27, 2021

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 :

How to Fix Same ID already exists error in Magento 2

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!

If you are looking for a Magento developer to fulfill your Magento 2 development needs, choose Dolphin Web Solution.

Happy Coding!

Jigar Patel

Author

We can help you with

  • Dedicated Team
  • Setup Extended Team
  • Product Development
  • Custom App Development

Schedule a Developer Interview And Get 7 Days Risk-Free Trial

Fill out the form below, and one of our technical experts will reach out to you within 12 hours.

    Google
    |

    4.8

    Google
    |

    4.8

    Google
    |

    4.9

    Google
    |

    4.8

    Google
    |

    4.9

    Copyright © 2024 DOLPHIN WEB SOLUTION. All rights reserved.

    ×

    Hello!

    Click one of our contacts below to chat on WhatsApp

    × How can I help you?
    TO TOP