我需要按类别名称检查类别是否存在。
我尝试使用类别名称 test 但它不起作用同时它不会抛出任何错误
$category = $this->_objectManager->create('Magento\Catalog\Model\Category');
$cate = $category->getCollection()->addAttributeToFilter('name','test')->getFirstItem();
if (!isset($cate)) {
echo "not exist";
} else {
echo "already exist";
}
如何处理是否存在类别?
谢谢
最新回答
- 2019-12-51 #
- 2019-12-52 #
如果条件如下,请进行更改:
if ($cate->getSize() > 0) { echo "already exist"; } else { echo "not exist"; }
上面的代码应该会给你想要的结果..
我建议你使用 Constructor Injection 获取集合而不是将其创建为对象管理器......
希望这会有用......
使用
$cate->getId()
当类别没有退出时,它会给出错误的值