Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何检查类别在Magento中是否可用?_Php_Magento_Categories - Fatal编程技术网

Php 如何检查类别在Magento中是否可用?

Php 如何检查类别在Magento中是否可用?,php,magento,categories,Php,Magento,Categories,在前端,我显示所有类别的第一级 现在我想检查是否有任何类别,无论是可用的或不在第一级 我正在使用此代码并获取所有类别 public function getCategory() { $parentCategoryId = Mage::app()->getStore()->getRootCategoryId(); $categories = Mage::getModel('catalog/category') ->g

在前端,我显示所有类别的第一级

现在我想检查是否有任何类别,无论是可用的或不在第一级

我正在使用此代码并获取所有类别

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";
    }

在您的回答中,您有类别id,但我没有类别id,那么您想如何区分类别是否存在?我的意思是,在第一级,任何类别是否可用?我正在尝试此代码,但如果类别不存在,它也会显示
已找到类别
,但它应该显示
未找到类别