在Magento的各个产品页面上制作一致的面包屑?

在Magento的各个产品页面上制作一致的面包屑?,magento,Magento,在Magento中,我们现在看到的是,各个产品页面上的面包屑总是根据用户到达页面的方式而变化。例如,如果用户从顶部类别一直单击到子类别,然后单击到产品,则面包屑将类似于“主页>>顶部类别>>子类别>>产品”” 但是,如果用户直接到达产品页面,例如从Google查询,面包屑将只是“主页>>产品””。像这样是不酷的 通过在面包屑中添加类别,即使用户从谷歌进入页面,这也肯定会改善用户体验。而且它肯定会增加每次访问的PV,因为用户可能会点击面包屑中的父类别 那么,有没有办法使其一致并始终在产品页面上显示

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

但是,如果用户直接到达产品页面,例如从Google查询,面包屑将只是“主页>>产品””。像这样是不酷的

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

那么,有没有办法使其一致并始终在产品页面上显示类别路径

我的想法是编辑page/html/breadcrumbs.phtml,但我不知道如何仅在产品页面上向breadcrumbs添加类别

任何帮助都将不胜感激

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

编辑:一句话,我只希望类别出现在产品页面的面包屑中,无论用户如何到达产品页面。哪一个类别无关紧要,只要有类别就行

有人知道吗?这有那么难吗


在我的脑海中,我需要编辑面包屑模板,并在其中插入类别(如果没有),这是产品页面…但我不知道如何,似乎在任何地方都找不到任何相关的解决方案…

我完全理解您想要做什么,我注意到,即使是从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().'/';
}

您可以重写产品模型以更改getProductUrl方法。基本上是获取该产品的第一个类别,并在产品路径之前附加类别路径。

@发布了一个解决方案,向breadcrumbs.phtml中添加一个片段,它可以正常工作

有关代码,请参见此处:

只需将此代码段添加到app/design/frontend/default/template_name/template/page/html/breadcrumbs.phtml的顶部

根据microtime(),添加的页面加载/PHP呈现时间仅为0.05秒。

将“app/code/core/Mage/page/Block/Html/Breadcrumbs.PHP”中的函数替换为该函数

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

找到app/code/core/Mage/Catalog/Helper/Data.php并将Data.php复制到 app/code/local/Mage/Catalog/Helper/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);
       }

     } 

为此,可以使用灯光扩展,如。请注意,已实现逻辑以获取分配产品的最低级别类别。您可以根据自己的需要轻松改变这种行为。

我认为您的想法不是每个人都想要的。很多网站在不同的类别下有相同的产品,所以按照你的逻辑,面包屑会给出不正确的信息。对于您的解决方案,我建议您将原来的面包屑块更改为您自己的自定义块,您将在其中显示类似于产品属性的内容。谢谢您的评论,但我认为这绝对是每个人都想要的想法。对不起,我没说清楚。我只需要分类,在面包屑里,不管是哪一类;Magento可以自己决定。现在的问题是,我的大多数访问者甚至看不到面包屑中的任何类别,因为他们都来自谷歌,直接进入产品页面……这既不方便用户使用,也不利于页面查看。直接访问时,默认情况下可能会显示ID最低的类别?还是随机的?两者都可以。不,你是对的,这不会添加到面包屑中,这是我用来复制面包屑的代码。然后,您可以创建自己的模块,在产品视图页面上显示面包屑。这段代码不需要加载整个类别模型(并且在循环中)。有比这更有效的方法;上面发布的代码只会增加页面加载时间。@sonassi很公平……提交您自己的答案让我们大家都可以学习怎么样?谢谢你,Blackburn,但我对Magento很不熟悉,因此,我不知道如何使用这段代码来实现我的目的…。。@kavoir.com您可以随时将其放在顶部的view.phtml中,以了解它的功能+1,将其保存在它所属的Page/Block/Html/Breadcrumbs.php中,而不是执行我正在恢复网站的标准Magento模板垃圾桶编程。很容易模块化。如何在不修改核心文件的情况下做到这一点?不适用于多层类别(至少在magento 1.9上)。