Skip to content

Instantly share code, notes, and snippets.

@kayintveen
Created June 14, 2015 10:26
Show Gist options
  • Save kayintveen/a5c762f565b42a107c88 to your computer and use it in GitHub Desktop.
Save kayintveen/a5c762f565b42a107c88 to your computer and use it in GitHub Desktop.
Magento removing product images for all storeviews
<?php
/**
*
* By Microdesign B.V.
* Author: Kay in 't Veen
* Twitter: @kayintveen
* More info: k.veen@microdesign.nl
*
*/
$products = Mage::getModel('catalog/product');
$prodIds=$products->getCollection()->getAllIds();
// Retrieve all products. You could populate the array from, for example, a list of SKUS if you only want to delete images on a subset
$prodIds=array($loadProduct->getId());
$nrOfProducts = count($prodIds);
$values = array(
'image'=>'no_selection',
'small_image'=>'no_selection',
'thumbnail'=>'no_selection',
);
//Populate this array with IDs of ALL your store fronts
$store_fronts = array(5,7,8,9);
foreach ($store_fronts as $store_front)
{
Mage::app()->getStore()->setId($store_front);
Mage::getSingleton('catalog/product_action')->updateAttributes($prodIds, $values, $store_front);
}
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
Mage::getSingleton('catalog/product_action')->updateAttributes($prodIds, $values, Mage_Core_Model_App::ADMIN_STORE_ID);
$counter = 0;
foreach($prodIds as $productId) {
++$counter;
$products = Mage::getModel('catalog/product');
$product = $products->load($productId);
$attrCode = 'media_gallery';
$mediaGalleryData = $product->getData($attrCode);
if (isset($mediaGalleryData['images'])) {
foreach ($mediaGalleryData['images'] as &$image) {
$image['removed'] = 1;
}
$product->setData($attrCode, $mediaGalleryData);
echo "Saving product ".$name." (".$counter." / ".$nrOfProducts.") \n";
try{
$product->save();
} catch (Exception $e)
{
echo "Exception saving ".$name.", skipping\n".$e->getMessage()."\n";
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment