天天看点

Magento产品页面包屑导航(Breadcrumb)修正 Showing Breadcrumbs Anywhere in Magento

Breadcrumbs are very useful for user navigation. Breadcrumbs for product page, category page, etc. are created by default Magento code.

The following code will show breadcrumbs created by Magento. You can print the following code anywhere in php or phtml files.

echo $this->getLayout()->getBlock('breadcrumbs')->toHtml();
           

You can create you own breadcrumbs as well. Like, you may need to create breadcrumbs if you have your own custom built module. I will show you here, how you can do it.

It’s simple and easy. At first, you will define the breadcrumbs block. Then, you will add label, title and link to your breadcrumbs. The addCrumb Magento function is used in this case.

$breadcrumbs = $this->getLayout()->getBlock('breadcrumbs');
         
$breadcrumbs->addCrumb('home', array('label'=>Mage::helper('cms')->__('Home'), 'title'=>Mage::helper('cms')->__('Home Page'), 'link'=>Mage::getBaseUrl()));
 
$breadcrumbs->addCrumb('country', array('label'=>'Country', 'title'=>'All Countries', 'link'=>'http://example.com/magento/moduleName/country'));
 
$breadcrumbs->addCrumb('manufacturer', array('label'=>'State', 'title'=>'States'));
 
echo $this->getLayout()->getBlock('breadcrumbs')->toHtml();
           

The label, title and link can be changed according to your need and requirement.

Hope this helps. Thanks.

来源: http://blog.chapagain.com.np/magento-easily-add-breadcrumbs-to-any-page/

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

Magento产品页面包屑导航(Breadcrumb)修正 Showing Breadcrumbs Anywhere in Magento

修正的方法,找到文件

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

来源:http://www.sqlstudy.com/article/magento-product-page-breadcrumb-add-category-path.html