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();
}
?>
asked Sep 21, 2018 at 18:30

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();
answered Sep 22, 2018 at 14:59
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. Commented 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 .. Commented Sep 24, 2018 at 5:50
  • Thanks! So that it would become: if (!is_null($thumbnail) and (trim($thumbnail) != '')) {? Commented 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.. Commented Sep 24, 2018 at 19:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.