I successfully hiden categories without any products from the navigation bar by following this helpful source http://prattski.com/2011/10/06/magento-module-hide-empty-categories/ and made a minor tweak inside Observer.php (as the solution of Prattski doesn't cover anchored categories and for my application I use image selection/presence to keep track of visibility of a product).
Anyways my question is this is slow: how do I optimize it?
=== STATISTICS ===
Page load time without cache without category filter = 3 secs
Page load time without cache with category filter = 9 secs
Page load time with cache with category filter = 6 secs
=== CODE CHANGE ===
public function _removeHiddenCollectionItems($collection)
{
// Loop through each category or product
foreach ($collection as $key => $item)
{
// If it is a category
if ($item->getEntityTypeId() == 3) {
$category = Mage::getModel('catalog/category')->load($item->getId());
$products = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($category)
->addAttributeToFilter('image', array('neq' => 'no_selection'));
$prodcount = $products->count();
/*if ($item->getProductCount() < 1) {
$collection->removeItemByKey($key);
}*/
if ($prodcount < 1){
$collection->removeItemByKey($key);
}
}
}
}
This code will work for anyone looking to remove the "No product in your selection" issue (only if you are not dealing with 25k+ products)... Hopefully someone out there could perfect it by optimizing the code to work faster!
Thanks