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

?

magento breadcrumb lost category path

?

修正的方法,找到文件

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

?

復(fù)制一份到local代碼池

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

?

在函數(shù)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);
       }
    }

    // ...

    
?

首先判斷當(dāng)前是否是產(chǎn)品頁,如果是并且沒有Category信息,就獲取產(chǎn)品所屬的Category IDs, Magento 中一個(gè)產(chǎn)品可以加入多個(gè)Category中,但不管三七二十一只挑出其中一個(gè)幸運(yùn)的Category作為current_category。看最終的效果:

magento breadcrumb including category path