如何检查类别在 Magento 中是否可用?
How to check category is available or not in Magento?
在前端我显示的是第一层的所有类别。
现在我想看看第一层有没有可用的类别。
我正在使用此代码并获取所有类别。
public function getCategory()
{
$parentCategoryId = Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')
->getCollection()
->addFieldToFilter('parent_id', array('eq'=>$parentCategoryId))
->addFieldToFilter('is_active', array('eq'=>'1'))
->addAttributeToFilter('level', 2)
->addAttributeToSelect('*');
return $categories;
}
如果没有可用的类别,那么我想显示一条消息,但我不知道如何在第一级检查类别是否可用。
判断类别是否存在于一级
$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')//or you can just add some attributes
->addAttributeToFilter('level', 2)//2 is actually the first level
->addAttributeToFilter('is_active', 1)//if you want only active categories
;
if(isset($categories) && !empty($categories->getData())) {
echo "Category found";
} else {
echo "Category not found";
}
在前端我显示的是第一层的所有类别。
现在我想看看第一层有没有可用的类别。
我正在使用此代码并获取所有类别。
public function getCategory()
{
$parentCategoryId = Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')
->getCollection()
->addFieldToFilter('parent_id', array('eq'=>$parentCategoryId))
->addFieldToFilter('is_active', array('eq'=>'1'))
->addAttributeToFilter('level', 2)
->addAttributeToSelect('*');
return $categories;
}
如果没有可用的类别,那么我想显示一条消息,但我不知道如何在第一级检查类别是否可用。
判断类别是否存在于一级
$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')//or you can just add some attributes
->addAttributeToFilter('level', 2)//2 is actually the first level
->addAttributeToFilter('is_active', 1)//if you want only active categories
;
if(isset($categories) && !empty($categories->getData())) {
echo "Category found";
} else {
echo "Category not found";
}