Skip to content

Products “X” and “Y” have the same set of attribute values

I’m doing something like this

 foreach ($arrayItemsToReplace as $key => $item){
                $product->getMediaGalleryImages()->removeItemByKey($key);
                $product->getMediaGalleryImages()->addItem($item);
            }
            if(count($arrayItemsToReplace)){
                try{
      
                    $this->productRepository->save($product);

But this is triggering me this error on the save action

Products “X” and “Y” have the same set of attribute values.

I’m also facing this issue with the same piece of code but more like on the addItem i think because this one make the script crash

In Collection.php line 408: Item (MagentoFrameworkDataObject) with the same ID “4053” already exists.

Any ideas ?

My goal with this piece of code is simply to update the products image name with is why i want to delete existing items and replacing them by (mostly the same item) but with a different file / path and url item

           $productGalleryImageItems = $product->getMediaGalleryImages()->getItems();

            $arrayItemsToReplace = [];
            foreach ($productGalleryImageItems as $key =>  $imageItem){


                try{


                    $imageItem->setData('file',$newFilename);
                    $imageItem->setData('path',$newPath);
                    $imageItem->setData('url',$newUrl);

                    $arrayItemsToReplace[$key] = $imageItem;