Magento中的子类别列表

时间:2013-11-29 17:58:24

标签: php magento categories

我经常环顾四周,发现很多关于提取父ID和子猫列表的信息,但这有点不同,我似乎无法找到答案,我不是一个PHP大师(还) 所以对我很轻松。我尝试了各种各样的东西,但我最终得到了错误。

我创建了一个phtml模板,用于在我的主导航下方的内联链接块中显示父类别的子类别。我正在使用来自管理员的静态块调用此模板,该工作正常,但是当我导航到子类别页面时链接块消失,显然是因为此代码调用父类的子猫而不是当您实际上是小猫。这是我到目前为止使用的代码:

<?php if (!Mage::registry('current_category')) return ?>
<?php $_categories = $this->getCurrentChildCategories() ?>
<?php $_count = is_array($_categories)?count($_categories):$_categories->count(); ?>
<?php if($_count): ?>
    <div class="category-products <?php echo Mage::getModel('catalog/layer')->getCurrentCategory()->getName(); ?>">
        <dl id="narrow-by-list2">
            <dt></dt>
            <dd>
                <ol class="subcat_list">
                <?php foreach ($_categories as $_category): ?>
                    <?php if($_category->getIsActive()): ?>
                    <li>
                        <a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>><?php echo $this->htmlEscape($_category->getName()) ?></a>
                    </li>
                    <?php endif; ?>
                <?php endforeach ?>
                </ol>
            </dd>
        </dl>
        <script type="text/javascript">decorateDataList('narrow-by-list2')</script>
    </div>
<?php endif; ?>

关于我如何修改这个的任何想法,所以当我实际查看一只子猫时,列表仍然存在? 非常感谢

2 个答案:

答案 0 :(得分:1)

经过多次环顾四周,我终于偶然发现了我需要的东西 感谢MagikSwapna的输入,它帮助我理解了更多的东西

我最终得到了这个

    <div class="category-products <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('parent_cat_name')->toHtml() ?>">
<?php  echo "<ol class='subcat_list'>"; ?>

 <?php
    $currentCat = Mage::registry('current_category');

    if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
    {
        // current category is a toplevel category
        $loadCategory = $currentCat;

    }
    else
    {
        // current category is a sub-(or subsub-, etc...)category of a toplevel category
        // load the parent category of the current category
        $loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
        // @TODO enhance for more nested category levels to display sub-categories
    }    
    $subCategories = explode(',', $loadCategory->getChildren());

    foreach ( $subCategories as $subCategoryId )
    {
        $cat = Mage::getModel('catalog/category')->load($subCategoryId);

        if($cat->getIsActive())
        {
            if($crcat == $cat->getName())                                                   //Check if current category is this subcategory
                echo '<li><b><a href="'.$cat->getURL().'">'.$cat->getName().'</a></b>'.'</li>'; //If yes display it as bold (Currently Selected)
            else                                                                            //
                echo '<li><a href="'.$cat->getURL().'">'.$cat->getName().'</a>'.'</li>';        //Otherwise display it as normal
        }
    }

?>
<?php  echo "</ol>"; ?>
</div>

并且它工作正常,我创建了一个用于类别页面的自定义布局,如果在非类别相关页面中使用它会抛出错误,但它可以工作! 最后。

答案 1 :(得分:0)

<?php $_helper = Mage::helper('catalog/category') ?>
    <?php $_categories = $_helper->getStoreCategories() ?>   
    <?php if (count($_categories) > 0): ?>
  <ul>
            <?php foreach($_categories as $_category): ?>
                    <li>                    
                    <a href="<?php echo $_helper->getCategoryUrl($_category) ?>"><?php echo $_category->getName() ?></a>                  
                    <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                    <?php $_subcategories = $_category->getChildrenCategories() ?>
                    <?php if (count($_subcategories) > 0): ?>                                                                                                                                    
                         <?php foreach($_subcategories as $_subcategory): ?>
                                <h3><a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"><?php echo $_subcategory->getName() ?></a></h3>
                                <!--sub sub category-->
                                <?php $_subcategory = Mage::getModel('catalog/category')->load($_subcategory->getId()) ?>                             
                                <?php $_subsubcategories = $_subcategory->getChildrenCategories() ?>
                                <?php if (count($_subsubcategories) > 0): ?>
                                   <ul>
                                    <?php foreach($_subsubcategories as $_subsubcategory): ?>
                                       <li>
                                          <a href="<?php echo $_helper->getCategoryUrl($_subsubcategory) ?>"><?php echo $_subsubcategory->getName() ?></a>            
                                          <?php $_subsubsubcategory = Mage::getModel('catalog/category')->load($_subsubcategory->getId()) ?>                             
                                          <?php $_subsubsubcategories = $_subsubcategory->getChildrenCategories() ?>
                                          <?php if (count($_subsubsubcategories) > 0): ?>                                                                                            
                                                   <ul>
                                                     <?php foreach($_subsubsubcategories as $_subsubsubcategory): ?>
                                                       <li>
                                                         <a href="<?php echo $_helper->getCategoryUrl($_subsubsubcategory) ?>"><span><?php echo $_subsubsubcategory->getName() ?></span></a>
                                                       </li> 
                                                     <?php endforeach; ?>
                                                   </ul>                                                                                               
                                          <?php endif; ?>
                                       </li> 
                                    <?php endforeach; ?>
                                   </ul>                                   
                               <?php endif; ?>
                                 <!--sub sub category-->                               
                         <?php endforeach; ?>                                                                                                                                                     
                    <?php endif; ?>
                    </li> 
             <?php endforeach; ?> 
  </ul> 
    <?php endif; ?>