I want to implement something that would delete the empty categories and sub categories if there are no products in them.
There may be 100s of categories.So I don't want to use backend for this.
I am in trouble please help me.
I am referring this link Hide Empty Categories but this is only hiding parent categories from navigation bar even if it's sub categories have products in them.
Here is one way to delete 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 - not simply hide them
EDIT
To answer the following posted in a comment:
"Could you please share a link of some tutorial or weblink.I am weak in creating custom modules".
see here
You would be better just creating a script for this simple task. Here is a nice resource to explain how to bootstrap Magento for your script to run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With