2013-08-30 3 views
1

Я стараюсь, чтобы мой большой каталог несколько дружественных планшетов, предлагая выбор вкладок с подуровнями для моих отдельных категорий. Поэтому, если пользователь нажимает на категорию заголовка (1-го уровня), ему нужно отобразить nr блоков, которые содержат изображение, описание и URL-адрес каждого прямого дочернего элемента и список всех дочерних категорий нижележащего (3-го уровня) показанного (2-й уровень) уровень). Кто-нибудь из вас, ребята, был бы таким добрым, чтобы проверить мой код?Как показать детскую подкатегорию текущей категории в Magento

<?php 
    $layer = Mage::getSingleton('catalog/layer'); 
    $_category = $layer->getCurrentCategory(); 
    $_categories = $_category->getCollection() 

->addAttributeToSelect(array('url_key','name','image','all_children','is_anchor','description')) 
    ->addAttributeToFilter('is_active', 1) 
    ->addIdFilter($_category->getChildren()) 
    ->setOrder('position', 'ASC') 
    ->joinUrlRewrite(); 
?> 

      <?php $children = explode(",", $this->getCurrentCategory()->getChildren()); ?> 

    <ul class="category-grid"> 
    <div class="category-list"> 
     <?php foreach($children as $child): ?> 
      <?php $_child = Mage::getModel('catalog/category')->load($child); ?> 
      <li class="item"> 
      <a href="<?php echo $_child->getURL() ?>" target="_self"><img title="<?php echo $this->htmlEscape($_child->getName()) ?>" src="<?php echo $this->htmlEscape($_child->getImageUrl()) ?>" alt="<?php echo $this->htmlEscape($_child->getName()) ?>" /></a> 
      <div class="subcategory-title"> 
     <a href="<?php echo $_child->getURL() ?>" title="<?php echo $this->htmlEscape($_child->getName()) ?>"><?php echo $this->htmlEscape($_child->getName()) ?></a> 
     </div> 
      <div class="description-block"> <?php echo $_child->getDescription(); ?></div> 
      <div class="children-links"><?php 
      $_helper = Mage::helper("catalog/category"); 
      $rootCat = Mage::app()->getStore()->getRootCategoryId(); 
      $current = Mage::registry('current_category'); 

       if ($child){ 
        //look for anchestor 
        $parentid = $child->getParentId(); 
        $parent = Mage::getModel("catalog/category")->load($parentid); 
        if($parentid != $rootCat) 
        { 
         //find the anchestor 
         show_cat($parent,$_helper,$rootCat); 
        }else{ 
         //is root 
      $_subcategories = $child->getChildrenCategories(); 
       echo $_child->getAll_Children(); 
       if(count($_subcategories)>0){ 
          echo '<ul>'; 
            foreach($_subcategories as $_category){ 
             echo '<li>'; 
             echo '<a href="'.$_helper->getCategoryUrl($_category).'">'.$_category->getName().'</a>'; 

              if($child->getId() == $_category->getId()){ 
               $current = Mage::registry('current_category'); 
               if ($current){ 
                //handle current 
                $_current_subcategories = $current->getChildrenCategories(); 
                 if(count($_current_subcategories)>0){ 
                  //the current cat has childrens 
                  echo '<ul>'; 
                  foreach($_current_subcategories as $_sub_category){ 
                   echo '<li>'; 
                   echo '<a href="'.$_helper->getCategoryUrl($_sub_category).'">'.$_sub_category->getName().'</a>'; 
                   echo '</li>'; 
                  } 
                  echo '</ul>'; 
                 }else{ 
                  //the current cat has no childrens 
                  $current_parent = $current->getParentId(); 
                  $current_parent = Mage::getModel("catalog/category")->load($current_parent); 
                  $_current_subcategories = $current_parent ->getChildrenCategories(); 
                  echo '<ul>'; 
                   foreach($_current_subcategories as $_sub_category){ 
                    echo '<li>'; 
                    echo '<a href="'.$_helper->getCategoryUrl($_sub_category).'">'.$_sub_category->getName().'</a>'; 
                    echo '</li>'; 
                   } 
                  echo '</ul>'; 
                  } 
                } 
               } 
             echo '</li>'; 
             } 
           echo '</ul>'; 
          } 
         } 
        } 

           ?> 
       </div> 
      </li> 
     <?php endforeach ?> 
     </div> 
    </ul> 
+0

ОК, а не над кодом, который я только что добавил: htmlEscape($_child->getChildren()) ?> который получит все подкатегории children_id. Любые пользователи о том, как заставить их отображать правильное имя/URL? –

ответ

1

вы можете это, используя код, а также смотрите ссылку в нижней

вы могли бы пойти с этим

<?php $_helper = Mage::helper('catalog/category') ?> 
<?php $_categories = $_helper->getStoreCategories() ?> 
<?php $currentCategory = Mage::registry('current_category') ?> 
<?php if (count($_categories) > 0): ?> 
    <ul class="category"> 
     <?php foreach($_categories as $_category): ?> 
      <li> 
       <a href="<?php echo $_helper->getCategoryUrl($_category) ?>"> 
        <?php echo $_category->getName() ?> 
       </a> 
       <?php if ($currentCategory->getId() && $currentCategory->getId() == $_category->getId()): ?> 
        <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?> 
        <?php $_subcategories = $_category->getChildrenCategories() ?> 
        <?php if (count($_subcategories) > 0): ?> 
         <ul class="subcategory"> 
          <?php foreach($_subcategories as $_subcategory): ?> 
           <li> 
            <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"> 
             <?php echo $_subcategory->getName() ?> 
            </a> 
           </li> 
          <?php endforeach; ?> 
         </ul> 
        <?php endif; ?> 
       <?php endif; ?> 
      </li> 
     <?php endforeach; ?> 
    </ul> 
<?php endif; ?> 

EDIT

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

Или вы можете go throw this Detail documentation, я уверен, что это было бы очень полезно для вас.

+0

Спасибо за ответ, но этот фрагмент кода просто дает все категории и не фильтрует их каким-либо образом, как в моем коде. Мне просто нужно показать детям 3-го уровня показания allready (проверьте мой код) 2 категории подуровня. –

+0

@SjorsVoorwinden, используйте это http://stackoverflow.com/questions/14580970/magento-3rd-level-subcategories-menu также http://www.pauldonnelly.net/magento-display-other-products-from-the- одинаковой категории/категории 3-го уровня. обязательно помогите вам. и я был бы рад, если вы примете мой ответ. – liyakat

+0

Я только что добавил htmlEscape($_child->getChildren()) ?> после childlinkds div. Это отображает только идентификатор, и мне нужен адрес для детей. Любые тысячи? –

0
// get current category 
$current_category = $layer->getCurrentCategory(); 

// get sub categories of current category 
$parent_categories = Mage::getModel('catalog/category')->getCategories($current_category->getId()); 

// go through each sub category and get their sub categories. 
foreach($parent_categories as $child_category) 
{ 
    $child_category_id = $child_category->getId(); 
    $grandchild_categories = Mage::getModel('catalog/category')->getCategories($child_category_id); 

} 
+0

Это может сработать для меня, будет проверять это –

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