in ,

How to show only Visible/Active Products in Product Count

Show Visible Products in Cart

Magento’s integrated function getProductCount() also counts invisible/inactive products. If your theme displays product counts next to each category (e.g. in the top menu or category blocks), this can be very confusing for your customer.

If your category product count shows 10 products, but 6 are set to invisible. Your customer wil end up very disappointed to see only 4 products in the product list.

Today I will show you how to display only visible/active products in your Magento theme’s category product count.

getProductCount uses the getData-method to retrieve the product_count value from Magento’s database. The problem with this is that Magento also takes invisible or inactive products into consideration when calculating the product count for a category.

To adjust this behaviour we need to add filters to the product collection as follows:

<?php
$collection = $_category->getProductCollection();
$collection->setVisibility(array(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG));
$collection->addFieldToFilter('status',Mage_Catalog_Model_Product_Status::STATUS_ENABLED);

If your theme uses a different variable name for the $_category, then replace it with the name your theme uses. You can then return the correct product count by using the $collection->count(). E.g. if you’d want to display the adjusted category count, you would add the following line of code in the right place of your theme’s file:

<?php echo $collection->count(); ?>

That’s it! Your theme will now only display visible/active products in category product count. This code can be used anywhere in your template.

Buy me a beer?

Do you appreciate my work and support? Please consider supporting me by donating, so I can continue to develop and write useful solutions for you.

Choose amount

Personal Message

Thank you! 🙂

Written by Daan van den Bergh

Magento 2 Back-end Developer with a passion for trainlifting, airplane-gliding, hunting trees and creating fake hobbies.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.