Last active
August 29, 2015 14:14
-
-
Save mklooss/75471c3a6eb1eb9da29a to your computer and use it in GitHub Desktop.
set Default Product Image, when it is not set yet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
set Default Product Image, when it is not set yet | |
*/ | |
if(!isset($argv)) | |
{ | |
die('only via cli'); | |
exit; | |
} | |
$dir = __DIR__; | |
chdir($dir); // change to magento dir | |
ini_set('display_errors', 1); | |
error_reporting(E_ALL | E_STRICT); | |
require_once 'app/Mage.php'; | |
umask(0); | |
Mage::app('admin'); | |
$collection = Mage::getModel('catalog/product')->getCollection() | |
->addAttributeToSelect(array('image', 'small_image', 'thumbnail')) | |
->addAttributeToFilter( | |
array( | |
array('attribute'=> 'image', 'like' => ''), | |
array('attribute'=> 'image', 'null' => true), | |
array('attribute'=> 'image', 'like' => 'no_selection'), | |
array('attribute'=> 'small_image', 'like' => ''), | |
array('attribute'=> 'small_image', 'null' => true), | |
array('attribute'=> 'small_image', 'like' => 'no_selection'), | |
array('attribute'=> 'thumbnail', 'like' => ''), | |
array('attribute'=> 'thumbnail', 'null' => true), | |
array('attribute'=> 'thumbnail', 'like' => 'no_selection'), | |
) | |
); | |
foreach($collection as $_product) | |
{ | |
$_product = Mage::getModel('catalog/product')->load($_product->getId()); | |
$attributesData = array(); | |
foreach($_product->getMediaGalleryImages() as $_image) | |
{ | |
$attributesData = array( | |
'image' => $_image->getData('file'), | |
'small_image' => $_image->getData('file'), | |
'thumbnail' => $_image->getData('file'), | |
); | |
Mage::getSingleton('catalog/product_action') | |
->updateAttributes(array($_product->getId()), $attributesData, 0); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment