查看内容

Magento快速删除所有除默认根目录分类

2017-9-14 17:19| 发布者: lutish| 查看: 530| 评论: 0

 
getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');

$state->setAreaCode('frontend');

$categoryFactory = $objectManager->get('Magento\Catalog\Model\CategoryFactory');
$newCategory = $categoryFactory->create();
$collection = $newCategory->getCollection();
$objectManager->get('Magento\Framework\Registry')->register('isSecureArea', true);

foreach($collection as $category) {

$category_id = $category->getId();

if( $category_id <= 2 ) continue;

try {
$category->delete();
echo 'Category Removed '.$category_id .PHP_EOL;
} catch (Exception $e) {
echo 'Failed to remove category '.$category_id .PHP_EOL;
echo $e->getMessage() . "\n" .PHP_EOL;
}
}



//比进入数据库清除来的安全,不会出现数据库中的错误。
返回顶部