0

Is there any pre-defined way in Magento to get all "viewable" products ?

I want to calculate total count of the products that are viewable in the category page. I know that in each category page, there is a current_category registry that we can use to calculate the number of products in the current category but I want a way to do it for all categories and store the results in a table for each category in each website-store.

The getProductCount not giving the exact number of viewable-only products also simple getProductCollection or similar functions don't return correct results.

Of course viewable products should be enabled, visible, In stock and with qty > 0 and for configurable products there might be at least 1 associated product that have above conditions in the current store view. But there might be stores that show Out of stock products also and label with "Out of Stock".

I want to calculate the exact number shown in category page, how can I do that ? Is there any easy way for this ? Or I need to write complex queries and retrieve data directly from the database?

i.e: I want the exact number calculated here in category page:

enter image description here

1

Yes, use this code

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('visibility', 4);

It will get you the visible products, since 4 means Catalog and search visibility. You can combine that with this to add stock check

Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($products);
  • No no, I mean the products that really visible in FE. I wrote an example. i.e A product maybe visible + enable but not in stock, so it won't show in FE. – Michel Gokan Jul 15 '14 at 19:05
  • You should AVOID by all means querying the DB since 99% of the features you will use to interact with the database are already covered. – mbalparda Jul 15 '14 at 19:06
  • the code you mentioned won't work for all visible+enabled+in stock+qty>0 products. Also consider the example I just wrote about configurable products which are special kind of products. Let me know if there is any pre-defined way of doing this. – Michel Gokan Jul 15 '14 at 19:07
  • Check my answer again, i just added some code to it. – mbalparda Jul 15 '14 at 19:08
  • Again your edit won't return all products that visible in FE. Tested . – Michel Gokan Jul 15 '14 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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