Last active
May 26, 2016 19:34
-
-
Save dfelton/7eaa12f41fdc8b9a5951312d853293e7 to your computer and use it in GitHub Desktop.
Finds simple products which belong to a configurable, and have no base image. Then assigns the base image from the configurable to that simple product.
This file contains hidden or 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 | |
/** | |
* Finds simple products which belong to a configurable, and have no base image. | |
* Then assigns the base image from the configurable to that simple product. | |
* | |
* @category FirstScribe | |
* @package FirstScribe_Shell | |
* @author Darren Felton | |
*/ | |
require 'abstract.php'; | |
class FirstScribe_Shell_SimpleOfConfigurableApplyBaseImage extends Mage_Shell_Abstract | |
{ | |
public function run() | |
{ | |
/* @var $configurables Mage_Catalog_Model_Product_Resource_Collection */ | |
/* @var $configurable Mage_Catalog_Model_Product */ | |
/* @var $type Mage_Catalog_Model_Product_Type_Configurable */ | |
/* @var $simples Mage_Catalog_Model_Product_Resource_Collection */ | |
/* @var $simple Mage_Catalog_Model_Product */ | |
ini_set('memory_limit', '512M'); | |
echo PHP_EOL; | |
echo 'Fetching products...',PHP_EOL; | |
$configurables = Mage::getResourceModel('catalog/product_collection') | |
->addAttributeToFilter('type_id', 'configurable') | |
->addAttributeToSelect('image'); | |
foreach ($configurables as $configurable) { | |
// If configurable this has no images, it won't help us anyway. | |
if (false == $configurable->getData('image') || 'no_selection' == $configurable->getData('image')) { | |
continue; | |
} | |
$type = Mage::getModel('catalog/product_type_configurable')->setProduct($configurable); | |
$simples = $type->getUsedProductCollection()->addAttributeToSelect('image'); | |
foreach ($simples as $simple) { | |
if (false == $simple->getData('image') || 'no_selection' == $simple->getData('image')) { | |
$filename = Mage::getBaseDir('media').DS.'catalog'.DS.'product'.$configurable->getData('image'); | |
if (file_exists($filename)) { | |
$simple->addImageToMediaGallery($filename, array('image','small_image','thumbnail'),false,false); | |
try { | |
$simple->save(); | |
echo 'Image added to simple product, ID: ',$simple->getId(),PHP_EOL; | |
} catch (Exception $e) { | |
echo $e->getMessage(),PHP_EOL; | |
} | |
} | |
} | |
} | |
} | |
echo 'SCRIPT COMPLETE',PHP_EOL,PHP_EOL; | |
} | |
} | |
$shell = new FirstScribe_Shell_SimpleOfConfigurableApplyBaseImage (); | |
$shell->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment