0

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.

1

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/

0

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.