【问题标题】:get all active categories and subcategories in product listing page [Magento]获取产品列表页面中的所有活动类别和子类别 [Magento]
【发布时间】:2015-01-28 04:24:04
【问题描述】:

我的类别和子类别层次结构到产品列表页面:

  • 产品
    • 冰箱
      • 后排冰箱
        • 产品列表(此处)

现在,我想要获得的是,当我在产品列表页面中时,我想获得所有带有相应链接的父类别和子类别。

我尝试的是:

$categories = Mage::getModel('catalog/category')->getCollection()
                            ->addAttributeToSelect('*')
                            ->addIsActiveFilter();

但这不会呈现在产品列表页面中。那么,我怎样才能在产品列表页面中获取所有活动类别和子类别的链接,这些链接会将我带到相应的类别/子类别页面。

【问题讨论】:

    标签: magento


    【解决方案1】:
    <?php $currentCategoryId = $this->getLayer()->getCurrentCategory()->getId();
            $curentCategory = Mage::getModel('catalog/category')->load($currentCategoryId);
            $path = $curentCategory->getPath();
            // $path will return category IDs like "1/5/16/30/45" : Here, 1 is the main root category & 2 is the Default magento root category. After these, you will get the category Ids by hierarchy . 
    
            /*
            * Output "1/5/16/30/45" will have following hierarchy 
            *1-
            *  2-
            *    16-
            *      30-
            *        45
            */
            // later you can explode by slash(/) and go ahead with your further display logics
    ?>
    

    【讨论】:

    • 那么,如何获取具有类别id的类别名称?
    • 一旦你将其分解为类别 ID 数组,加载类别并使用 foreach 中的以下内容获取名称:$name = Mage::getModel('catalog/category')->load(id)- >getName();
    • 好的,但它转义了中间子类别。 :( 它给了我 1/3/444(空白/根目录/产品)它逃脱了层次结构中的所有子类别。在我的情况下,它逃脱了 regrigerator/back-bar fridges
    • 请检查后台的分类层次。如果问题仍然存在,请在加载的当前类别对象上检查另一个方法“->getParentCategories()”。
    • $path = $curentCategory-&gt;getParentCategories()-&gt;getPath(); 致命错误:调用成员函数getPath()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-02
    • 2014-08-20
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    相关资源
    最近更新 更多