Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Filter Product Collection by Enabled Only
 
fxchain
Member
 
Avatar
Total Posts:  40
Joined:  2009-03-01
 

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
 
Avatar
Total Posts:  2205
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);

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

This method has been deprecated

 
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:  214
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() ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
miked2004
Sr. Member
 
Avatar
Total Posts:  214
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());
 
Magento Community Magento Community
Magento Community
Magento Community
 
Clay S
Jr. Member
 
Total Posts:  15
Joined:  2009-10-02
 

I achieved this like so:

$products Mage::getModel('catalog/product')->getCollection()
    ->
addAttributeToSelect('*')
    ->
addFieldToFilter('status',Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
 
Magento Community Magento Community
Magento Community
Magento Community
 
MattStephens
Sr. Member
 
Avatar
Total Posts:  152
Joined:  2011-07-12
United Kingdom
 

Hello. Just an update on this…

As miked2004 has suggested, these methods are now depreciated.

Product Status

Instead of:

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

You can use:

$collection->addFieldToFilter('status'Mage_Catalog_Model_Product_Status::STATUS_ENABLED)

Other options avaliable are:
STATUS_ENABLED
STATUS_DISABLED

Product Visibility

Instead of:

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

You can use:

$collection->addFieldToFilter('visibility'Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);

Other options avaliable are:
VISIBILITY_NOT_VISIBLE
VISIBILITY_IN_CATALOG
VISIBILITY_IN_SEARCH
VISIBILITY_BOTH

For more information have a look at:
/core/Mage/Catalog/Products/Visibility.php
/core/Mage/Catalog/Products/Status.php

You will find the contstants defined at the top of each of these.

Other filtering

For that matter, you can also filter this way for the product type (simple, configurable etc.). Take a peak at /core/Mage/Catalog/Products/Type.php

Cheers
Matt

 
Magento Community Magento Community
Magento Community
Magento Community
 
jrosell
Jr. Member
 
Avatar
Total Posts:  24
Joined:  2009-10-05
Barcelona
 

I wonder if the following code is deprecated. Is workng on magento 1.7.0.2

Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);
 
Magento Community Magento Community
Magento Community
Magento Community
 
MattStephens
Sr. Member
 
Avatar
Total Posts:  152
Joined:  2011-07-12
United Kingdom
 

Hello JRosell

You’re correct, the addInStockFilterToCollection() method is still accessible against the Catalog Inventory Stock singleton.

The methods described as depreciated in the post mainly concern product visibility rather than stock, therefore the method you\’ve chosen still remains an acceptable way to check a product is in stock or not.

Cheers

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top