2

如何在 Magento 的类别页面上仅显示父类别和子类别?

单击子类别时,导航必须保持不变。

示例:如果我有

第一类

  • 子目录 1
  • 子目录 2
  • 子类 3

单击任何子类别(例如 subcat 1)后,我想要相同的,即:

第一类

  • 子目录 1
  • 子目录 2
  • 子类 3

我现在得到的:

<?php
 if (Mage::registry('current_category'))
{
  echo Mage::registry('current_category')->getName();
} 
?> 

 <?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())
        {
            echo '<a href="'.$cat->getURL().'">'.$cat->getName().'</a> ';
        }
    }
?>

但是当然这不正确,但我找不到一个好的解决方案,以便在我按下 subcat 1 时类别 1 保持不变。

如果所选的子类别是粗体的,那就太好了。

4

1 回答 1

1

在您的代码中,代替

<?php
 if (Mage::registry('current_category'))
    {
      echo Mage::registry('current_category')->getName();
    } 
?> 

尝试使用

<?php
function getParentTopCategory($c)
    {
        if($c->getLevel() == 2){
            return $c;
        } else {
            $parentCategory = Mage::getModel('catalog/category')->load($c->getParentId());
            return getParentTopCategory($parentCategory);
        }
    }

 if (Mage::registry('current_category'))
    {
      $category = Mage::registry('current_category');
      $t = getParentTopCategory($category);
      echo $t->getName();
    } 
?>

它应该工作。

编辑:这是您的完整解决方案:-

$crcat = Mage::registry('current_category')->getName(); //The current category name is stored in $crcat
function getParentTopCategory($c)
    {
        if($c->getLevel() == 2){
            return $c;
        } else {
            $parentCategory = Mage::getModel('catalog/category')->load($c->getParentId());
            return getParentTopCategory($parentCategory);
        }
    }

 if (Mage::registry('current_category'))
{
  $category = Mage::registry('current_category');
        $t = getParentTopCategory($category);
        if($crcat == $t->getName())                 //Check if current category is the topmost category
            echo "<b>".$t->getName()."</b>";        //If yes display it as bold (Currently Selected)
        else                                        //
            echo $t->getName();                     //Otherwise display it as normal

        echo "<br>";
} 
?> 

 <?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 '<b><a href="'.$cat->getURL().'">'.$cat->getName().'</a></b>'.'</br>'; //If yes display it as bold (Currently Selected)
            else                                                                            //
                echo '<a href="'.$cat->getURL().'">'.$cat->getName().'</a>'.'</br>';        //Otherwise display it as normal
        }
    }
于 2013-06-18T08:23:38.007 回答