Magento product images duplicate issue with CSV product importer

Posted by Damodar Bashyal on January 28, 2014

 

Magento duplicate image issue and solution

Our client likes to leave images on media/import folder and when they import csv, if product image already existed, it was creating duplicate image.

So below is a quick workaround to fix this issue. Here we are just deleting previous images if we are going to re-import them again.

There could be a simpler solution but this worked for me.

@file app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php
// @method public function saveRow( array $importData )

$attributes = $product->getTypeInstance()->getSetAttributes();
if (isset($attributes['media_gallery'])) {
    $gallery = $attributes['media_gallery'];
    //Get the images
    $galleryData = $product->getMediaGallery();
    $csvImages = array();
    if ( !empty( $importData['gallery'] ) ) {
        $csvImages = explode( ',', $importData["gallery"] );
    }
    $csvImages[] = $importData['image'];
    foreach($csvImages as $img){
        $importDataImage = explode('/',trim($importData['image'],'/'));
        $importDataImage = array_pop($importDataImage); // get filename part only
        $importDataImage = str_replace('\\', '/', Mage_Core_Model_File_Uploader::getDispretionPath($importDataImage) . '/' . $importDataImage); // replace windows directory_separator with linux one
        $galleryData['images'][]['file'] = $importDataImage; /* add this new image file from csv to list of images that needs to be removed if that exists. */
    }
    // now delete all previous images.
    if (!empty($galleryData)) {
        foreach ($galleryData['images'] as $image) {
            //If image exists
            if ($gallery->getBackend()->getImage($product, $image['file'])) {
                $gallery->getBackend()->removeImage($product, $image['file']);
            }
            $imageOldFile = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'product' . $image['file'];
            /*delete any previous image imports, that still exists on the server*/
            if (file_exists($imageOldFile)) {
                unlink($imageOldFile);
            }
        }
    }
}

After you delete previous images, you can proceed with core code that will re-import image from media/import.

arpit raka posted on - Thursday 5th of February 2015 06:16:50 AM

how to used this code plz tell me
 
not published on website


QR Code: Magento product images duplicate issue with CSV product importer