magento product collection filter by status and visibility

piaoling  2011-11-01 11:22:03

Hello,

I am trying to create a product feed. When I load the product collection, any filter I try to not include disabled product do not work.

Code:

<?php
require_once 'app/Mage.php';
umask(0);
Mage::app('default');

$products Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect('*');
//$products->addAttributeToFilter('status', array('value'=>1));
//$products->addAttributeToFilter('status', array('eq'=>'1'));
//$products->addAttributeToFilter('status', 1);

//Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
   

$products->load();
foreach(
$products as $product{

    
echo $product->getData('name')."<br />";
    
//var_dump($product->getStatus());
}
?>

As you can see I try to filter them different ways (they are now commented out, but I tried them one by one):
$products->addAttributeToFilter(’status’, array(’value’=>1));
$products->addAttributeToFilter(’status’, array(’eq’=>’1’));
$products->addAttributeToFilter(’status’, 1);
Mage::getSingleton(’catalog/product_status’)->addVisibleFilterToCollection($products);

Also, $product->getStatus() always returns 1, even if the product is disabled

o_0

Please help!  (looks like it adds to all single and double quote on this forum)

 
Magento Community Magento Community
Magento Community
Magento Community
 
The Sunday Paper
Sr. Member
 
Total Posts:  202
Joined:  2008-08-06
 

Mage::getSingleton(’catalog/product_status’)->addVisibleFilterToCollection($collection);
Mage::getSingleton(’catalog/product_visibility’)->addVisibleInCatalogFilterToCollection($collection);

 
Magento Community Magento Community
Magento Community
Magento Community
 
fxchain
Member
 
Avatar
Total Posts:  40
Joined:  2009-03-01
 

Thank, that worked!!!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sana-Sbiha
Member
 
Total Posts:  61
Joined:  2009-10-13
 

Hello all,

I m trying to filter product by status. I used this code:

$product=Mage::getRessourcesModel(’catalog/product_collection’)
->addAttributeToSelect(’*’)
->addAttributeToFilter(’status’,1)

But i have all product affiched even wich have status disable. And when i do echo for the sattus i have always 1.

How did you resolve the problème?

Thks a lot

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Total Posts:  1085
Joined:  2009-11-18
Bucharest, Romania
 

Read the thread more carefully.
See what ‘The Sunday Paper’ suggested.
instead of
->addAttributeToFilter(’status’,1)
use
Mage::getSingleton(’catalog/product_status’)->addVisibleFilterToCollection($collection);

It’s not a bug. It’s a feature.
http://marius-strajeru.blogspot.com/
E-commerce solutions and much more: Anais-IT

 
Magento Community Magento Community
Magento Community
Magento Community
 
Netismine
Sr. Member
 
Total Posts:  163
Joined:  2008-10-28
 

This method has been deprecated

Netismine.com
Magento custom development

 
Magento Community Magento Community
Magento Community
Magento Community
 
Devz
Jr. Member
 
Total Posts:  7
Joined:  2010-12-06
 

Is there any new non-depreciated method for doing this?

Im trying to export my product list, but status=1 regardless of if the product is enabled or not.

 
Magento Community Magento Community
Magento Community
Magento Community
 
dam358
Jr. Member
 
Total Posts:  6
Joined:  2010-03-29
 

+1
I’m interested to filter by status

 
Magento Community Magento Community
Magento Community
Magento Community
 
anoopnath
Jr. Member
 
Total Posts:  14
Joined:  2010-08-27
 

Hello

Please tell me at which level status filter is done for Catalog/product collection.
is it at the level of Varien classes?..
as in the Product status class the function is deprecated.

Thanks in advance
Anoop

 
Magento Community Magento Community
Magento Community
Magento Community
 
miked2004
Sr. Member
 
Avatar
Total Posts:  205
Joined:  2007-12-13
Atlanta, Georgia
 

Any news not this. All methods I have found are deprecated. Should we just use the attribute filter? addAttributeToSelect() ?

~ Mike D
http://www.sharpdotinc.com
http://www.sharpdotinc.com/mdost

 
Magento Community Magento Community
Magento Community
Magento Community
 
miked2004
Sr. Member
 
Avatar
Total Posts:  205
Joined:  2007-12-13
Atlanta, Georgia
 

Just an update:

This will do nothing in new releases

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);

This will filter the collection by visibility status:

Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

Both of the above are deprecated but the second one will filter the collection. You can also do things like the following to filter based on different combinations of the products visibility setting.

$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());

//OR

$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInSiteIds());

~ Mike D
http://www.sharpdotinc.com
http://www.sharpdotinc.com/mdost

 

类别 :  magento(258)  |  浏览(12134)  |  评论(0)
发表评论(评论将通过邮件发给作者):

Email: