首页 文章

显示Magento面包屑中产品的最长路径

提问于
浏览
2

我的情况如下:

  • 我在主页上显示我的'Recommended'类别的产品 .

  • 推荐产品也包含在不同的类别和子类别中 .

  • 我找到了一种流行的解决方案,可以在面包屑中显示产品的完整路径 . 我将以下代码添加到app / code / local / Mage / Catalog / Block / Breadcrumbs.php:

$current_category   = Mage::registry('current_category');
$current_product    = Mage::registry('current_product');
if(!$current_category && $current_product){
    $categories = $current_product->getCategoryCollection()->addAttributeToSelect('name')->setPageSize(1);
    foreach($categories as $category) {
        Mage::unregister('current_category');
        Mage::register('current_category', $category);
    }
}
  • 因此,我在产品页面上看到了面包屑中的完整路径:主页>推荐>产品名称

但我希望展示产品的完整路径,包括其“实际”类别,而非推荐 . 如何强制不在面包屑中显示推荐类别,但是显示,例如,产品的“最长途径”?

1 回答

  • 0

    好吧,我会说你仍然从这个解决方案中得到推荐的类别,因为推荐的类别实际上是这个或那些产品出现的类别列表中的第一个 .

    也就是说,您实际上可以使用 addAttributeToFilter 从集合中排除某些结果 .

    $current_category   = Mage::registry('current_category');
    $current_product    = Mage::registry('current_product');
    if(!$current_category && $current_product){
        $categories = $current_product->getCategoryCollection()
                                      ->addAttributeToSelect('name')
                                      ->addAttributeToFilter(
                                          'entity_id',
                                          array( 'neq' => $current_category->getId() )
                                      )
                                      ->setPageSize(1);
        /**
         * so we just get the id of the current category (the recommended category)
         * and we exclude it out of the category collection via "neq", for "not equal"
         **/
    
        /* You don't really need this foreach, setPageSize(1) actually means : 
           add "limit 1" to my sql query
    
        foreach($categories as $category) {
            Mage::unregister('current_category');
            Mage::register('current_category', $category);
        } */
    
        Mage::unregister('current_category');
        Mage::register('current_category', $categories->getFirstItem());
    }
    

相关问题