2011-08-05 36 views
3

所以我想这个代码显示在app /前端/预设/默认/目录/类我view.phtml页上的问题儿童和他们的孩子/显示所有类别的儿童和他们的孩子在Magento

所以当你看到的类别页面,你看到所有的子类别的所有孩子

这是我得到的,它是显示子类别,但不是他们的孩子。

<?php 
    $_category = $this->getCurrentCategory(); 
    $collection = Mage::getModel('catalog/category')->getCategories($_category-  >entity_id); 
    $helper  = Mage::helper('catalog/category'); 
    ?> 

    <ul> 
    <?foreach ($collection as $cat):?> 
      <?php if($_category->getIsActive()):?> 
      <?php 
       $cur_category = Mage::getModel('catalog/category')->load($cat->getId()); 
       $_img = $cur_category->getImageUrl(); 
      ?> 
      <li> 
          <a href="<?php echo $helper->getCategoryUrl($cat);?>"> 
           <img src="<?php echo $_img?>" title="$cat->getName()"/> 
           <cite><?php echo $cat->getName();?></cite> 
          </a> 
        </li> 
      <?php endif?> 

<?php endforeach;?> 

+1

管理中的类别是否设置为“锚点”?这通常会使他们自动包括他们的大孩子。 – clockworkgeek

+0

它们被设置为“否”。 –

回答

7

请尝试使用以下代码。 主要想法是获取主要级别类别。为每个类别获得儿童类别。

<?php 
     /* Get the categories that are active for the store */ 
     $_main_categories=$this->getStoreCategories(); 

     /* Get the current category the user is in */ 
     $_current_category=$this->getCurrentCategory(); 

     /* Get the current category path */ 
     $_categorypath = $this->getCurrentCategoryPath(); 
?> 

    <?php if ($_main_categories): ?> 
     <div class="box normal-nav"> 
      <div class="box-top"> 
      </div> 
      <div class="box-content"> 
        <ul> 
         <?php 
          /* This bit cycles through the categories - setting the next one to current */ 
          foreach ($_main_categories as $_main_category): 
           if($_main_category->getIsActive()): 
            $cur_category=Mage::getModel('catalog/category')->load($_main_category->getId()); 
            $layer = Mage::getSingleton('catalog/layer'); 
            $layer->setCurrentCategory($cur_category); 
         ?> 

            <li><a href="<?php echo $this->getCurrentCategory()->getUrl()?>"><?php echo $this->getCurrentCategory()->getName();?></a> 

             <?php $_maincategorylisting=$this->getCurrentCategory()?> 

             <?php $_categories=$this->getCurrentChildCategories()?> 

             <?php if($_categories->count()): ?> 
              <ul class="subcategory"> 
               <? foreach ($_categories as $_category):?> 
                <? if($_category->getIsActive()): 
                  $cur_subcategory=Mage::getModel('catalog/category')->load($_category->getId()); 
                  $layer = Mage::getSingleton('catalog/layer'); 
                  $layer->setCurrentCategory($cur_subcategory); 
                ?> 

                  <li><a href="<?php echo $this->getCategoryUrl($_category)?>"> <?php echo $_category->getName()?></a></li> 
                <? endif;?> 

               <?endforeach?> 

              </ul> 
              <?php $layer->setCurrentCategory($_current_category); ?> 

             <? endif; ?> 
            </li> 

          <?php endif; ?> 

         <?php endforeach; ?> 
        </ul> 
       </div> 
       <div class="box-bottom"> 

       </div> 
     </div> 
    <?php endif; ?> 

后来补充:

如果申请修复您的代码看起来如下:

<?php 
     $_category = $this->getCurrentCategory(); 
     $collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id); 
     $helper  = Mage::helper('catalog/category'); 
    ?> 
    <ul> 
    <?php foreach ($collection as $cat):?> 
      <li> 
       <a href="<?php echo $helper->getCategoryUrl($cat);?>"> 
        <cite><?php echo $cat->getName();?></cite> 
       </a> 
       <?php $childLevel2Category = Mage::getModel('catalog/category')->getCategories($cat->entity_id); ?> 
       <ul> 
        <?php foreach ($childLevel2Category as $catLevel2) { ?> 
         <li> 
          <a href="<?php echo $helper->getCategoryUrl($catLevel2);?>"> 
           <cite><?php echo $catLevel2->getName();?></cite> 
          </a> 
         </li> 
        <?php } ?> 
       </ul> 
      </li> 
<?php endforeach;?> 
    </ul> 

如果您需要更多级别(更多的子DIR)使用递归函数重写这个结构。

+0

此代码无效。分类没有显示,也没有他们的潜艇。 –

+0

请尝试底部提供的新解决方案。 –

+0

这工作。谢谢! –

3

尝试改变“是锚”设置这些父类别为“是”。这应该允许他们在没有任何自定义编码的情况下显示其子类别的产品。

+0

这没有与我使用的这段代码做到这一点。 –

0

我知道这是一个古老的问题,但我在搜索过程中碰到它,试图做同样的事情。

为了未来访问者的利益,上面的答案之一谈到了将父类别的Is Anchor更改为Yes,以便向孩子展示。 OP说它不适合他。

重要的是,这样做后,你需要重新索引你的类别/产品关联(系统 - >索引管理)。如果您有很多产品,这可能需要一些时间。

但是这样做后......瞧!列出后裔产品。

而且在模板/目录/类别将这个代码http://www.creare.co.uk/magento-subcategories-category-pages/list.phtml了显示子猫科动物的护理:

<?php 
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory(); 
$categories = $category->getCollection() 
     ->addAttributeToSelect(array('name', 'thumbnail')) 
     ->addAttributeToFilter('is_active', 1) 
     ->addIdFilter($category->getChildren()) 
?> 
<ul class="subcategories"> 
    <?php foreach ($categories as $category): ?> 
     <li> 
      <a href="<?php echo $category->getUrl() ?>"><img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getThumbnail() ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" /> 
       <span><?php echo $category->getName() ?></span></a> 
     </li> 
    <?php endforeach; ?> 
</ul> 

希望这可以帮助别人。

+1

这有效,但是在控制器/帮助类中的代码版本会更好。 – juz

相关问题