1

我想删除空类别和子类别。我尝试了以下脚本。但它没有从管理面板中删除类别。我的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();
    }
}
4

2 回答 2

0

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

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

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

于 2013-06-07T08:02:01.350 回答
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();
    }
}
于 2015-06-19T10:45:34.023 回答