5

我有类别 id。我从这段代码中得到了 id

<?php echo $current_catid=$this->getCategoryId(); ?> 

现在我想检查这个类别是否有子类别

如果它有子,它将显示子类别图像和名称和 url。

4

5 回答 5

8

如果你有 current_category id 然后加载类别

$category = Mage::getModel('catalog/category')->load(id);

并检查count($category->getChildren());

其他方法适用于count children

count($category->getChildrenNodes()); 

$category->getChildrenCount();

这样您就可以检查类别是否有孩子。

getChildren()方法为您提供儿童类别 ID,并且基于 id 您可以获得类别图像和类别名称。

于 2013-03-29T12:24:54.903 回答
5

请试试这个,它在我的最后工作正常

<?php  
$parentCategoryId = 10;
$categories = Mage::getModel('catalog/category')->load($parentCategoryId)->getChildren();
$catArray = explode(',', $categories);
foreach($catArray as $child)
{
$_child = Mage::getModel( 'catalog/category' )->load( $child );
echo $_child->getName() . '<br />';
echo $_child->getUrl() . '<br />';
echo $_child->getDescription() . '<br />';
}
?>
于 2013-03-29T13:11:22.887 回答
1

有点旧,但我正在寻找相同的解决方案,@Mufaddal 的解决方案不起作用。然后我发现getChildrenCategories()

$_category = Mage::registry('current_category');
count($_category->getChildrenCategories());
于 2015-02-26T15:22:30.853 回答
1

实际上,这取决于是否启用了“使用平面目录类别”选项。

因此,检查类别是否有子类别的最佳方法是:

if (Mage::helper('catalog/category_flat')->isEnabled()) {
    $childrenCount = $category->getResource()->getChildrenAmount($category);
} else {
    $childrenCount = $category->getResource()->getChildrenCount();
}

使用 $category 我想你已经有了,如:

$category = Mage::getModel('catalog/category')->load(id);
于 2015-11-25T10:17:03.250 回答
0

您还有另一个选项来检查类别子类别是否存在..

  <?php
    $currentCategoryId = Mage::registry('current_category')->getId();
    $collection = Mage::getModel('catalog/category')->getCollection()
        ->addAttributeToFilter('is_active', 1) //only active categories
        ->addAttributeToFilter('parent_id', $currentCategoryId);
    $currentCat = Mage::registry('current_category');
    $subCategories = Mage::getModel('catalog/category')->load($currentCat->getParentId())->getChildrenCategories();

    if($collection->getSize() >= 1){//there some thing....}else{

//Get Subcategory....


foreach ($subCategories as $subCategoryId ): 

                     if($subCategoryId->getIsActive())
                    {  $products = Mage::getModel('catalog/category')->load($subCategoryId->getId())
                    ->getProductCollection()
                    ->addAttributeToSelect('entity_id')
                    ->addAttributeToFilter('status', 1)
                    ->addAttributeToFilter('visibility', 4);


                        <li <?php if($subCategoryId->getId()==$currentCategoryId){?>class="active"<?php } ?>>
                            <a href="<?php echo $subCategoryId->getURL(); ?>">
                                <?php //echo $subCategoryId->getName()." (".$products->count().")"; ?>
                                <?php echo $subCategoryId->getName(); ?>
                            </a>
                        </li>
                      } endforeach;}?>

如果它有帮助,请告诉我......

谢谢拉维

于 2015-04-15T10:41:43.110 回答