2

Something went wrong with the import of many products. Only the small image and thumbnail are selected.

Now I want to set the base image the same as the small image and thumbnail.

enter image description here

How can I achieve this?

I tried this, but that does not work.

<?php 
require 'app/Mage.php';
Mage::app();

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
foreach ($products as $product) {
    if (!$product->hasImage()) {
        $product->setImage($product->getSmallImage());
    }
$product->save();
}
?>

1 Answer 1

1

The code below works for me. It is very fast to run and will not suffer from any third-party module getting in the way of updating your image field either for the fact only the attribute is saved rather than the whole product model.

Instead of filtering by skus like I do below in my script, you can filter the collection in many ways. You may try it first with a sku (like below) and then on the full collection or by set of 1000 products.. good luck

<?php
$path = __DIR__ . '/../';
$shell = 'shell/abstract.php';
$i = 0;
while (! file_exists($path . $shell) && ! file_exists($path . 'htdocs/' . $shell) && $i++ < 15) {
    $path .= '../';
}
chdir(file_exists($path . $shell) ? $path : $path . 'htdocs');

require_once $shell;

class Image_Repair_Test extends Mage_Shell_Abstract
{
    public function run()
    {
        $skus = [122663];

        $collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToFilter('sku', $skus)
            ->addAttributeToSelect('thumbnail')
            ->addAttributeToSelect('image');
         //$collection->setPageSize($limit);
         //$collection->setCurPage($page);
        if ($collection->count() > 0) {
            foreach ($collection as $product) {
                $image = $product->getImage();
                $thumbnail = $product->getThumbnail();
                if (($image == 'no_selection') and !is_null($thumbnail) and (trim($thumbnail) != '')) {
                    $product->setImage($thumbnail)->getResource()->saveAttribute($product, 'image');
                }
            }
        }
    }
}

$shell = new Image_Repair_Test();
$shell->run();
4
  • Thanks a lot! I tried it, but it does not seems to change any product. I removed the ->addAttributeToFilter('sku', $skus) line to select all products, but it does not work.
    – JGeer
    Sep 23, 2018 at 21:16
  • You may try to change / remove the if with ‘no_selection’. Or at least you should be able to debug and tell which part is not working. I use this script all the time .. Sep 24, 2018 at 5:50
  • Thanks! So that it would become: if (!is_null($thumbnail) and (trim($thumbnail) != '')) {?
    – JGeer
    Sep 24, 2018 at 6:31
  • you need to either setup a debugger or add echo line to debug, my solution is pretty simple but yet you need to of course understand the code in it.. Sep 24, 2018 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.