I'm trying to get all product without image to desactivate them. I started from this thread https://stackoverflow.com/questions/3565377/how-can-i-find-all-products-without-images-in-magento
At first, it was just to extract products without images, and it was working fine, but now I need to disable them and it's half working.
The problem is, when the code return that the status has changed, when I go back in Magento Admin, the product is still enable. (cache and indexes refreshed)
But when I run again the code, the status has changed and I can't see the product on the site.
So the code is working BUT I don't know why the admin product manager can't see that change.
I didn't find threads where I can get a glimpse of a solution. Maybe I'm just doing all wrong and I can't PROPERLY change the status from a collection.
Here my php file (mymagento/var/import/dev/test.php) :
header('Content-Type: text/html; charset=utf-8');
require_once('../../../app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
$storeId = 1; //check in magento admin
echo "Store ID : ".$storeId."\n<br />\n";
//this builds a collection that's analagous to
//select * from products where image = 'no_selection'
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter(
array(
array (
'attribute' => 'image',
'like' => 'no_selection'
),
array(
'attribute' => 'image',
'null' => '1'
),
array(
'attribute' => 'small_image',
'null' => '1'
),
array(
'attribute' => 'thumbnail',
'null' => '1'
),
array(
'attribute' => 'image',
'nlike' => '%/%/%'
),
array(
'attribute' => 'small_image',
'nlike' => '%/%/%'
),
array(
'attribute' => 'thumbnail',
'nlike' => '%/%/%'
)
),
null,
'left'
);
echo "SKU;NAME;VISIBLE;STATUS\n<br />\n";
foreach($products as $product)
{
$product->load();
if($product->getVisibility() == 4 && $product->getStatus() == 1) {
echo $product->getSku() . ";".$product->getName().";".$product->getVisibility().";".$product->getStatus()."\n<br />\n";
try {
Mage::getModel('catalog/product_status')->updateProductStatus($product->getId(), $storeId, Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
echo "Product #".$product->getSku()." : Status changed to ".$product->getStatus().". \n<br />\n";
} catch(Exception $e) {
echo "Product #".$product->getSku()." Status could not be changed: ".$e->getMessage()."\n<br />\n";
}
}
try {
$product->save();
echo "Product #".$product->getSku()." : saved\n<br />\n";
} catch ( Exception $e ) {
echo "Product #".$product->getSku()." could not be saved: ".$e->getMessage();
}
}
It's just a script to run 1 time to get rid of products without images to start on good bases for the next update.
Thank you for your time !