Thursday, November 28, 2013

Magento - PHP Script to Delete ALL Categories with without any Products

Magento - PHP Script to Delete ALL Categories with NO Products


<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

// Load Up Magento Core
define('MAGENTO', realpath(''));

require_once(MAGENTO . '/app/Mage.php');

$app = Mage::app();

$categoryCollection = Mage::getModel('catalog/category')->getCollection()
    ->addFieldToFilter('level', array('gteq' => 2))
;

foreach($categoryCollection as $category) {
    if ($category->getProductCount() === 0) {
        $category->delete();
    }
}
?>

No comments:

Post a Comment