Removing empty categories in magento

I want to implement something that removes empty categories and subcategories if there are no products in them.

There can be 100 categories. So I do not want to use a backend for this.

I have problems, please help me.

I refer to this link Hide empty categories , but it only hides the parent categories from the navigation bar, even if the subcategories have products in them.

+4
source share
1 answer

Here is one way to remove empty categories ...

$categoryCollection = Mage::getModel('catalog/category')->getCollection() ->addFieldToFilter('level', array('gteq' => 2)) ; foreach($categoryCollection as $category) { if ($category->getProductCount() === 0) { $category->delete(); } } 

This will delete the categories - don't just hide them


EDIT

To respond to the following comment, follow these steps:

 "Could you please share a link of some tutorial or weblink.I am weak in creating custom modules". 

see here

It is best to create a script for this simple task. Here is a good resource to explain how to run Magento to run your script.

+10
source

All Articles