Magento產品頁面的面包屑導航很怪異:如果從Category產品列表中進入Product,則面包屑導航中含有Category Path; 否則,當從首頁,或搜索結果中,或者其他什么地方進入,則缺少之。我想,可能是Magento支持一個產品放入多個Category的緣故吧。不管怎么 樣,產品頁中缺少了Category Path,用戶體驗不大好。如下:

?

magento breadcrumb lost category path

?

修正的方法,找到文件

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 including category path