3

Is there anyway i can hide a product that doesn't have a price or 0 for its price?

We use stock import tools (Magmi Data Importer) and so some products imported don't have a price. How can i check each product has a price and disable it if it doesn't.

I've tried overriding the getStatus() method from Mage Core but this seems to disable the whole product and not just a product which is part of a group.

2 Answers 2

4

Run this after the import:

$zeroPriceProducts = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter(
          array(
             array('attribute'=>'price', 'eq'=>'0'),
             array('attribute'=>'price', 'isnull'=>true),
          )
    );
$ids = $zeroPriceProducts->getAllIds();
Mage::getSingleton('catalog/product_action')->updateAttributes(
    $ids,
    array('status' => 2).
    0
)

This should identify the products with price zero and disable them.

9
  • Hi Marius. This doesn't seem to find 0 priced and instead is printing out every product in the DB. It seems that the attribute to select doesn't do anything?
    – Marc
    Nov 5, 2014 at 12:10
  • @Marc Sorry I should be addAttributeToFilter instead of addAttributeToSelect. I fixed it.
    – Marius
    Nov 5, 2014 at 12:14
  • Hi Marius, Yep i swapped this just after posting and also added '' around the 0? It seems to work now
    – Marc
    Nov 5, 2014 at 12:15
  • If this answer or any other one solved your problem, mark it as accepted. Let's keep a clean website.
    – Marius
    Nov 5, 2014 at 12:16
  • Just marked it solved as a quick one to check for NULL can i just add ->addAttributeToFilter('price', array('eq'=> NULL)); that way if the price doesn't exist also (which is mainly the case) it would also get disabled?
    – Marc
    Nov 5, 2014 at 12:18
1

I would create a backend script to fix this issue

require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 
Mage::app();
$Products = Mage::getModel('catalog/product')
                        ->getCollection()
                        ->addAttributeToSelect('*')
                        ->addPriceData()
                        ->addAttributeToFilter('price', array('lt' => '0.01') // you can change this accordingly
                        ->addFieldToFilter('status', array('eq' => '1');

foreach($Products as $product){
   $product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
   $product->save();
}

Remember to backup your db before run this script

1
  • Good answer except one thing. You should not use save in a loop. It's time and resource consuming. You don't need it if you are going to change one single attribute for all the products.
    – Marius
    Nov 5, 2014 at 11:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.