Magento产品页面的面包屑导航很怪异:如果从Category产品列表中进入Product,则面包屑导航中含有Category Path; 否则,当从首页,或搜索结果中,或者其他什么地方进入,则缺少之。我想,可能是Magento支持一个产品放入多个Category的缘故吧。不管怎么 样,产品页中缺少了Category Path,用户体验不大好。如下:

 

Magento产品页面包屑导航(Breadcrumb)修正 Showing Breadcrumbs Anywhere in Magento_第1张图片

 

修正的方法,找到文件

app/code/core/Mage/Catalog/Helper/Data.php

 

复制一份到local代码池

app/code/local/Mage/Catalog/Helper/Data.php

 

在函数getBreadcrumbPath的开始部分,加上如下的代码逻辑:

/**
 * Return current category path or get it from current category
 * and creating array of categories|product paths for breadcrumbs
 *
 * @return string
 */
public function getBreadcrumbPath()
{
    // added by p.c.w.l 20110603
    if ($this->getProduct() && !$this->getCategory()) {
       $_categoryIds = $this->getProduct()->getCategoryIds();

       if ($_categoryId = $_categoryIds[0]) {
          $_category = Mage::getModel('catalog/category')->load($_categoryId);
          Mage::register('current_category', $_category);
       }
    }

    // ...
 

首先判断当前是否是产品页,如果是并且没有Category信息,就获取产品所属的Category IDs,Magento 中一个产品可以加入多个Category中,但不管三七二十一只挑出其中一个幸运的Category作为current_category。看最终的效果:

Magento产品页面包屑导航(Breadcrumb)修正 Showing Breadcrumbs Anywhere in Magento_第2张图片