I have a sidebar in the homepage which displays all categories and I want to hide all categories and subcategories that are blank.
I'm using version 1.9.2 and tried Prattski but somehow it's not working. The module doesn't geshow.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI have a sidebar in the homepage which displays all categories and I want to hide all categories and subcategories that are blank.
I'm using version 1.9.2 and tried Prattski but somehow it's not working. The module doesn't geshow.
Well you need to check if the category/subcategory has products directly from category collection.
You can override Mage_Catalog_Model_Category
in local codePool or in a custom module.
Add a new method hasProducts
in overrided Model file like below:
public function hasProducts() {
//$products = Mage::getModel('catalog/category')->load($category_id)
$products = $this
->getProductCollection()
->addAttributeToSelect('entity_id')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4);
return ( $products->count() > 0 ) ? true : false;
}
Sample script(put in your site root folder) depicting, how to use above function:
<?php
$magePath = 'app/Mage.php';
require_once $magePath;
Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app();
$categories = Mage::getModel('catalog/category')->getCollection();
foreach($categories as $category):
if($category->hasProducts()) $catIds[] = $category->getId();
endforeach;
echo "<pre/>";print_r($catIds);die;
Refer below links to check how to override Models: http://nickbartlett.com/override-the-magento-category-model-and-resource-model/
http://magedev.com/magento-overriding-model-block-or-helper/