2012-09-14 31 views
11

在Magento中,我们现在拥有的是,单个产品页面上的面包屑总是根据用户到达页面的方式而变化。例如,如果用户从顶级类别一直点击到子类别,然后再点击到产品,则面包屑将类似于“家>>顶级类别>>子类别>>产品”。在Magento的各个产品页面上制作一致的面包屑?

但是,如果用户在产品页面直接到达,如谷歌查询,面包屑,简直是“首页>>产品”。像这样,并不令人满意。

即使用户到达Google的页面,通过在面包屑中添加类别也肯定会改善用户体验。而且它肯定会增加每次访问的PV,因为用户可能会点击面包屑中的父类别。

那么有没有什么办法让它保持一致并始终在产品页面上显示类别路径?

我的想法是编辑page/html/breadcrumbs.phtml,但我不知道如何将类别添加到面包屑只在产品页面上。

任何帮助,将不胜感激!

======================================

编辑:在单词,无论用户如何到达产品页面,我只想让类别出现在产品页面上的面包屑中。只要类别在哪里,哪些类别无关紧要。

任何人有任何想法?这很难吗?

关闭我的头顶,我将需要编辑面包屑模板,并在其中注入类别,如果没有任何,它是产品页面......但我不知道如何,我可以“T似乎找到任何相关的解决方案,这在任何地方......

+0

我不认为你的想法是每个人都会想要的。很多网站在不同类别下都有相同的产品,因此使用您的逻辑面包屑会导致错误的信息。对于您的解决方案,我建议您使用自己的自定义块更改原始的Breadcrumb块,并在其中显示产品属性等内容。 –

+1

感谢您的评论,但我认为这绝对是每个人都想要的想法。对不起,我没有说清楚。我只需要在那里的类别,在面包屑中,哪些类别无关紧要; Magento可以自己决定。现在的问题是,我的大多数访问者甚至都没有看到面包屑中的任何类别,因为它们都是从谷歌到达并直接登陆产品页面......它既不便于用户使用,也不适合页面浏览。在直接访问时,可能会默认显示ID最低的类别?或随机?两者都很好。 –

回答

16

用这个替换“app/code/core/Mage/Page/Block/Html/Breadcrumbs.php”中的_toHtml()函数。

protected function _toHtml() {    

    $cat_id = ""; 

    if (Mage::registry('current_product')) { 
     $product_id = Mage::registry('current_product')->getId(); 
     $obj = Mage::getModel('catalog/product'); 
     $_product = $obj->load($product_id); // Enter your Product Id in $product_id 

     if ($product_id) { 
     $categoryIds = $_product->getCategoryIds(); 
     $cat_id = $categoryIds[0]; 
     } 

     $category = Mage::getModel('catalog/category')->load($cat_id); 
     $cat_name = $category->getName(); 
     $cat_url = $this->getBaseUrl().$category->getUrlPath(); 
    } 

    if (is_array($this->_crumbs)) { 
     reset($this->_crumbs); 
     $this->_crumbs[key($this->_crumbs)]['first'] = true; 
     end($this->_crumbs); 
     $this->_crumbs[key($this->_crumbs)]['last'] = true; 
    } 

    if($cat_id) { 
     $this->_crumbs['category'.$cat_id] = array('label'=>$cat_name, 'title'=>'', 'link'=>$cat_url,'first'=>'','last'=>'','readonly'=>''); 
     ksort($this->_crumbs); 
     $home = $this->_crumbs['home']; 
     unset($this->_crumbs['home']); 
     array_unshift($this->_crumbs,$home); 
    } 

    $this->assign('crumbs', $this->_crumbs); 
    return parent::_toHtml(); 
} 
+0

+1将它保留在它所属的Page/Block/Html/Breadcrumbs.php中,而不是执行标准Magento模板Trashcan编程我正在从中恢复我们的网站。很容易模块化。 –

+0

如何在不修改核心文件的情况下做到这一点? – Raffael

2

我完全理解你想要做什么,我已经注意到了,甚至从Magento的自己的搜索链接的产品将失去它的类面包屑。我同意Slayer,我只会为产品页面实现您自己的自定义块。

您可以使用以下代码检索产品类别树。我已经测试了这个产品(在Magento CE 1.7.0.2中),只有一个类别层次结构的产品和那些具有多个类别层次结构的产品。

注意:你可能会想要做这个代码的一些工作,因为它的装载每一类对象到一个数组,这可能成为相当内存密集型。我会建议将只需到一个关联数组的信息,然后补充说阵列主阵列

$productCategories = array(); 

// grab the products category id array...we only want one, so grab the first one 
$categoryIds = $_product->getCategoryIds(); 
$categoryId = $categoryIds[0]; 

// we don't want the root category as the name may not be user friendly 
while ($categoryId != Mage::app()->getStore()->getRootCategoryId()) 
{ 
    // load the category object and add it to the product categories array 
    $currentCategory = Mage::getModel('catalog/category')->load($categoryId); 
    $productCategories[] = $currentCategory; 
} 

// as the categories were added in reverse order we'll need to "unreverse" them 
foreach (array_reverse($productCategories) as $category) 
{ 
    echo $category->getName().'/'; 
} 
+0

我不认为这会给面包屑添加任何东西吗? –

+0

不,你说得对,这不会添加到面包屑,这是我用来复制面包屑的代码。然后,您可以创建自己的模块,以在产品视图页面上显示面包屑。 – CCBlackburn

+0

此代码不必要地加载整个类别模型(并在此循环中)。对此有更有效的方法;上面发布的代码只会增加页面加载时间。 –

3

您可以改写的产品型号来改变getProductUrl方法。基本上获得该产品的第一类并在产品路径之前附加类别路径。

0

@jacek_podwysocki发布了一个解决方案,将一个片段添加到breadcrumbs.phtml,它的工作原理。

看到这里的代码:Programmatically add breadcrumbs paths in Magento?

就在这个片段添加到应用程序/设计/前端/默认/ TEMPLATE_NAME /模板/页/ HTML/breadcrumbs.phtml

添加的页面加载顶部/根据microtime(),PHP渲染时间仅为0.05秒。

1

查找应用程序/代码/核心/法师/目录/助手/ Data.php和Data.php复制到 应用程序/代码/本地/法师/目录/助手/ Data.php 查找功能公众功能getBreadcrumbPath(),下面的代码添加到该函数

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); 
     } 

    } 
+0

对于多层类别不起作用(至少在magento 1.9上)。 –

相关问题