【问题标题】:Print Category in tree structure magento在树结构magento中打印类别
【发布时间】:2015-08-27 07:02:50
【问题描述】:

我使用以下代码以树形结构打印类别,但它什么也没打印。它没有显示任何类别或子类别。

这是我的代码:

       <!-- List all categories and their second level subcategories -->
       <div class="block block-list block-categories">
    <div id="block-categories" class="block-title active">
    <strong><span>Categories </span></strong>
    </div>

 <div id="leftnav" class="block-content" style="display:block">
<?php $helper = $this->helper('catalog/category') ?>
<?php $categories = $this->getStoreCategories() ?>
<?php if (count($categories) > 0): ?>
    <ul id="leftnav-tree" class="level0">
        <?php foreach($categories as $category): ?>
            <li class="level0<?php if ($this->isCategoryActive($category)): ?> active<?php endif; ?>">
               <a href="<?php echo $helper->getCategoryUrl($category) ?>"><span><?php echo $this->escapeHtml($category->getName()) ?></span></a>
                <?php //if ($this->isCategoryActive($category)): ?>
                    <?php $subcategories = $category->getChildren() ?>
                    <?php if (count($subcategories) > 0): ?>
                        <ul id="leftnav-tree-<?php echo $category->getId() ?>" class="level1">
                            <?php foreach($subcategories as $subcategory): ?>
                                <li class="level1<?php if ($this->isCategoryActive($subcategory)): ?> active<?php endif; ?>">
                                    <a href="<?php echo $helper->getCategoryUrl($subcategory) ?>"><?php echo $this->escapeHtml(trim($subcategory->getName(), '- ')) ?></a>
                                     <?php $secondLevelSubcategories = $subcategory->getChildren() ?>
                                     <?php if (count($secondLevelSubcategories ) > 0): ?>
                        <ul id="leftnav-tree-<?php echo $subcategory->getId() ?>" class="level2">
                            <?php foreach($secondLevelSubcategories as $secondLevelSubcategory ): ?>
                                <li class="level2<?php if ($this->isCategoryActive($secondLevelSubcategory )): ?> active<?php endif; ?>">
                                    <a href="<?php echo $helper->getCategoryUrl($secondLevelSubcategory ) ?>"><?php echo $this->escapeHtml(trim($secondLevelSubcategory ->getName(), '- ')) ?></a>
                                </li>
                                <?php endforeach; ?>
                        </ul>
                        <script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
                    <?php endif; ?>
                            <?php endforeach; ?>
                        </ul>
                        <script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
                    <?php endif; ?>
                <?php //endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
    <script type="text/javascript">decorateList('leftnav-tree', 'recursive')</script>
<?php endif; ?>
 </div>
  </div>

谁能帮我打印树结构中的类别.. 提前致谢。

【问题讨论】:

标签: magento


【解决方案1】:

您可以使用以下代码来显示类别树。

<?php
$rootcatId= Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCategories($rootcatId);
function  get_categories($categories) {
    $array= '<ul>';
    foreach($categories as $category) {
        $cat = Mage::getModel('catalog/category')->load($category->getId());
        $count = $cat->getProductCount();
        $array .= '<li>'.
        '<a href="' . Mage::getUrl($cat->getUrlPath()). '">' .
                  $category->getName() . "(".$count.")</a>\n";
        if($category->hasChildren()) {
            $children = Mage::getModel('catalog/category')->getCategories($category->getId());
             $array .=  get_categories($children);
            }
         $array .= '</li>';
    }
    return  $array . '</ul>';
}
echo  get_categories($categories); ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-31
    • 2022-09-27
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多