Magento - 检查当前类别中是否存在子类别

时间:2014-05-26 12:26:10

标签: magento categories

我的magento商店中有以下类别设置:

store root
|
bedroom    kitchen     bathroom    
|            |            |            
furniture  furniture   furniture         
lighting   lighting    misc
misc   

我需要能够检查类别misc是否作为我当前类别中的子类别存在,如果是,则显示特定块。

所以,如果我在厨房类别,它不会显示,但在卧室和浴室,它会。

我该怎么做这个检查?

3 个答案:

答案 0 :(得分:1)

获取您的子类别misc

$currentCategoryId = 10; 
$collection = Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToFilter('is_active', 1) //only active categories
    ->addAttributeToFilter('parent_id', $currentCategoryId)
    ->addAttributeToFilter('name', 'misc');

检查您的收藏集中是否包含至少1条记录:

if ($collection->getSize() > = 1) {
    //'misc' sub-category exists
}
else {
    //'misc' sub-category does not exists
}

答案 1 :(得分:1)

使用加载当前类别并获取其ID

$currentCat = Mage::registry('current_category');
$currentCatId = $currentCat->getId();

使用

加载'misc'子类别
$_category = Mage::getModel('catalog/category')->loadByAttribute('name', 'misc');
$_categoryParentId = $_category->getParentCategory()->getId();

if( $currentCatId == $_categoryParentId){
//Do your stuffs
}

虽然您有两个名称为misc的类别,但您希望按url-key添加类别,或者通过多个类别数组进行解析以进行测试。

答案 2 :(得分:0)

尝试以后,

<?php  
$categoryId = 5;
$categories = Mage::getModel('catalog/category')->load($categoryId)->getChildren();
$catAsArray = explode(',', $categories);
//you can have $catAsArray to check if it has subcategories
foreach($catAsArray as $child)
{
    $_child = Mage::getModel( 'catalog/category' )->load( $child );
    echo $_child->getName() . '<br />';
    echo $_child->getUrl() . '<br />';
    echo $_child->getDescription() . '<br />';
}
?>

或者反过来说,

<?php

$categoryId = 5;
$_category  = Mage::getModel('catalog/category')->load($categoryId);

if($_category->hasChildren())
{


}
?>
相关问题