删除类别方法不起作用

时间:2013-06-07 07:42:31

标签: magento collections magento-1.7 categories catalog

我想删除空类别和子类别。我尝试了以下脚本。但它不会从管理面板中删除该类别。我的magento版本是1.7.0.2

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

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

2 个答案:

答案 0 :(得分:0)

您需要明确要求将产品计数添加到您的类别集合中:

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

否则$category->getProductCount()将返回null,而不是=== 0

答案 1 :(得分:0)

尝试

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

foreach($categoryCollection as $category) {
    if (!$category->getProductCount()) {
        $category->delete()->save();
        //$category->setId($category->getId())->delete();
    }
}