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.

Marc
  • 33
  • 1
  • 3

2 Answers2

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.

Marius
  • 197,939
  • 53
  • 422
  • 830
  • 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 05 '14 at 12:10
  • @Marc Sorry I should be addAttributeToFilter instead of addAttributeToSelect. I fixed it. – Marius Nov 05 '14 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 05 '14 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 05 '14 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 05 '14 at 12:18
  • @Marc. I modified the filter parameter. Not it should look for products with price zero or NULL. I didn't test it though. But it should work. – Marius Nov 05 '14 at 12:29
  • hi marius this is working but also disabling products where the special price is NULL any ideas? – Marc Nov 05 '14 at 16:26
  • Is it possible to modify the attribute, say price, which is different for products with the updateAttributes function ? Have a set of product ids and their corresponding price. – ted Jan 25 '16 at 08:21
  • I'm trying to get this to work for me as well but I can't get it to work. @Marius – Head Mar 22 '16 at 22:53
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

MagePal Extensions
  • 13,911
  • 2
  • 33
  • 52
  • 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 05 '14 at 11:50