【问题标题】:Magento display all categories on product view page with parent categoriesMagento 在带有父类别的产品视图页面上显示所有类别
【发布时间】:2012-03-02 10:27:29
【问题描述】:

接着这个问题:Display ALL categories that a product belongs to in Magento

有没有办法显示完整的类别路径(每个阶段都有链接),而不是只显示产品所属的最终类别?

到目前为止我有这个代码......

<?php
            $currentCatIds = $_product->getCategoryIds();
            $categoryCollection = Mage::getResourceModel('catalog/category_collection')
                 ->addAttributeToSelect('name')
                 ->addAttributeToSelect('url')
                 ->addAttributeToFilter('entity_id', $currentCatIds)
                 ->addIsActiveFilter();
            foreach($categoryCollection as $cat){
            ?>
                <a href="<?php echo $cat->getUrl(); ?>">
                    <?php echo $cat->getName() ?>
                </a>
            <?php } ?>

正确链接页面上显示的类别名称。 我想要的是显示完整的 Cat > Sub Cat > Sub Sub Cat 轨迹,并正确链接该轨迹中的每个元素。

【问题讨论】:

    标签: magento magento-1.4 categories


    【解决方案1】:

    这个怎么样:

    foreach($categoryCollection as $cat){
        $parents = $cat->getCollection()
            ->addIdFilter($cat->getParentIds())
            ->addAttributeToSelect('name')
            ->addUrlRewriteToResult()
            ->setOrder('level');
        foreach ($parents as $parentCat) {
            // Build your parent links
        }
    }
    

    顺便说一句,这种代码不属于模板。它应该进入正在渲染的块的方法(或至少进入帮助程序)。

    【讨论】:

    • 谢谢!这让我走上了正确的道路——我现在有了我需要的输出:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多