1

I want to update the image of one category shoes .In this category there are 10000 products , I want to update the image of those products only which have nike brand in their name , using script.

can any one help to write this script.

    <?php

require_once '../app/Mage.php';

Mage::app();
Mage::setIsDeveloperMode(true);

ini_set('display_errors', 1);

    $needle = 'divya';  

 $collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*') // select all attributes
->setPageSize(5000) // limit number of results returned
->setCurPage(1); // set the offset (useful for pagination)
 $xProd=$collection->addAttributeToFilter('name', array(
    array('like' => '% '.$needle.' %'), 
    array('like' => '% '.$needle), 
    array('like' => $needle.' %') 
));

// we iterate through the list of products to get attribute values
foreach ($xProd as $product) {
  echo "<br>Product Name :: ".$product->getName(); //get name


        $images = array(
            'thumbnail'   => 'newimg.png',
            'small_image' => 'newimg.png',
            'image'       => 'newimg.png',
        );

        $dir = Mage::getBaseDir('media') . DS . 'replaceimg/';

        foreach ($images as $imageType => $imageFileName) {

          echo "<br>".$path = $dir . $imageFileName;
            if (file_exists($path)) {
                try {

                    $product->addImageToMediaGallery($path, $imageType, false);
                } catch (Exception $e) {
                    echo $e->getMessage();
                }
            } else {
                echo "Can not find image by path: `{$path}`<br/>";
            }
        }
}

UPDATE 1.

Above script gives me the desired products but image is not replacing, and no error is thrown.

  • Please share code which you have tried already and what issue you are facing with it? – Jaimin Sutariya Aug 1 '17 at 3:51
  • @JaiminSutariya i updated my post. I am able to get those products but dont know how to update/replace the image of these products – inrsaurabh Aug 1 '17 at 4:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.