Magento filter products collection get only visible

You want to retrieve all your products but only the one visible in catalog or filter ?

You may use this

1
2
3
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

But it’s depreacated and doesn’t work.
You may instead use this :

1
2
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
$collection->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);

坚持原创技术分享,您的支持将鼓励我继续创作!