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($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

実際には、「フラット カタログ カテゴリを使用する」オプションが有効になっているかどうかによって異なります。

したがって、カテゴリに子カテゴリがあるかどうかを確認する最良の方法は次のとおりです。

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 に答える
1

少し古いですが、同じ解決策を探していましたが、@Mufaddal の解決策は機能しませんでした。それから私は見つけましgetChildrenCategories()た。

$_category = Mage::registry('current_category');
count($_category->getChildrenCategories());
于 2015-02-26T15:22:30.853 に答える
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 に答える