Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting empty categories in magento

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.

like image 396
Mukesh Avatar asked Dec 26 '22 18:12

Mukesh


1 Answers

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.

like image 110
Drew Hunter Avatar answered Dec 29 '22 06:12

Drew Hunter