2014-01-14 6 views
0

Как продлить Категория Уровень в Magento Frontend Сайт:Расширенная Категория уровня в Magento

Вставьте этот код * под первый уровень категории. В моем случае, категория сайта navbar находится на header.phtml

* Код начинается с: Уровень 2-й старт * Этот код высоко оценен.

(mikequibin/приложение/дизайн/интерфейс/mikequibin/по умолчанию/шаблон/страницы/HTML/header.phtml)

<div class="header-category-menu" style="background: none;"> 
      <?php $_helper   = Mage::helper('catalog/category') ?> 
      <?php $_categories  = $_helper->getStoreCategories() ?> 
      <?php $currentCategory = Mage::getSingleton('catalog/layer')->getCurrentCategory()->getId(); ?> 
      <?php if (count($_categories) > 0): ?> 
      <div class="menu-bg"> 
       <ul class="menu clearfix"> 
        <?php foreach($_categories as $_category): ?> 
        <?php $class = 'link'; ?> 
        <?php if($_category->getEntityId() == 13 || $_category->getEntityId() == 36): ?> 
        <?php $class = 'link sale'; ?> 
        <?php endif; ?> 
        <?php $class .= $currentCategory==$_category->getEntityId() ? ' active' : NULL; ?> 
        <li class="<?php echo $class; ?> menu-nav" data-id="<?php echo $_category->getId(); ?>"> 
         <?php if($_category->getId() != 36) : ?> 
         <i class="icon-caret-down"></i> 
         <?php endif; ?> 
         <a href="<?php echo $_helper->getCategoryUrl($_category) ?>"> 
          <span><?php echo $_category->getName() ?></span> 
         </a> 

         <!-- First Level Start --!> 

         <?php 
          $subCategories = Mage::getModel('catalog/category')->load($_category->getId()); 
          $subCategories = $subCategories->getChildrenCategories(); 
         ?> 
         <?php if(count($subCategories) != 0): ?> 
         <ul class="submenu"> 
         <?php foreach($subCategories as $i => $subCategory): ?> 
          <li> 
           <a href="<?php echo $_helper->getCategoryUrl($subCategory); ?>"> 
           <i class="icon-chevron-right"></i><?php echo $subCategory->getName(); ?> 

            <!-- Second Level Start --!> 
            <?php if($_category->getIsActive()): ?> 
             <?php 
             $subCategories2 = Mage::getModel('catalog/category')->load($subCategory->getId()); 
             $subCategories2 = $subCategories2->getChildrenCategories(); 
             ?> 
             <?php if(count($subCategories2) != 0): ?> 
              <?php foreach($subCategories2 as $k => $subCategory2): ?> 
               <li style="padding-left:20px;"> 
                <a href="<?php echo $_helper->getCategoryUrl($subCategory2); ?>"> 
                <?php echo $subCategory2->getName(); ?> 

                 <!-- Third Level Start --!> 
                 <?php if($_category->getIsActive()): ?> 
                 <?php 
                  $subCategories3 = Mage::getModel('catalog/category')->load($subCategory2->getId()); 
                  $subCategories3 = $subCategories3->getChildrenCategories(); 
                 ?> 
                  <?php if(count($subCategories3) != 0): ?> 
                   <?php foreach($subCategories3 as $l => $subCategory3): ?> 
                   <li style="padding-left:30px;"> 
                    <a href="<?php echo $_helper->getCategoryUrl($subCategory3); ?>"> 
                     <?php echo $subCategory3->getName(); ?> 
                    </a> 
                   </li> 
                   <?php endforeach; ?> 
                  <?php endif; ?> 
                <?php endif; ?> 
                <!-- Third Level End --!> 

                </a> 
               </li> 
              <?php endforeach; ?> 
             <?php endif; ?> 
            <?php endif; ?> 
            <!-- Second Level End --!> 

           </a> 
          </li> 

         <?php endforeach; ?> 
         </ul> 
         <?php endif; ?> 

         <!-- First Level End --!> 
        </li> 
        <?php endforeach; ?> 
       </ul> 
      </div> 
      <?php endif; ?> 
     </div> 
+0

Что здесь вопрос? – Marius

+0

Поскольку код работает, как мне улучшить код? Или есть альтернативные решения? Спасибо –

ответ

2
$_helper   = Mage::helper('catalog/category'); 
$_categories  = $_helper->getStoreCategories(); 

$html = ''; 
if (count($_categories) > 0) { 
    $html .= '<ul>'; 
    foreach($_categories as $_category) { 
     $html .= '<li>'; 
      $html .= '<a href="#">'.$_category->getName().'</a>'; 
      $html .= getRecursiveHtml($_category); 
     $html .= '</li>'; 
    } 
    $html .= '</ul>'; 
} 

function getRecursiveHtml($_category,$html='') { 
    $_category = Mage::getModel('catalog/category')->load($_category->getId()); 
    $child = $_category->getChildrenCategories(); 
    if (count($child) > 0) { 
     $html .= '<ul>'; 
     foreach($child as $_child) { 
      $html .= '<li>'; 
      $html .= '<a href="#">'.$_child->getName().'</a>'; 
      $html = getRecursiveHtml($_child,$html); 
      $html .= '</li>'; 
     } 
     $html .= '</ul>'; 
    } 
    return $html; 
} 

echo $html; 
+0

Спасибо! Оно работает. @Chirag –

Смежные вопросы