【问题标题】:Making consistent breadcrumbs on individual product pages in Magento?在 Magento 的各个产品页面上制作一致的面包屑?
【发布时间】:2012-09-14 02:51:51
【问题描述】:

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

但是,如果用户直接到达产品页面,例如通过 Google 查询,则面包屑将只是“首页 >> 产品”。而且这样也很不酷。

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

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

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

任何帮助将不胜感激!

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

编辑:总之,无论用户如何到达产品页面,我都希望类别出现在产品页面的面包屑中。不管是哪个类别,只要类别存在即可。

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

在我的脑海中,我需要编辑面包屑模板并在其中注入类别(如果没有的话)并且它是产品页面......但我不知道怎么做,我可以'似乎在任何地方都找不到任何相关的解决方案......

【问题讨论】:

  • 我不认为你的想法是每个人都会想要的。许多网站在不同类别下都有相同的产品,因此使用您的逻辑面包屑会提供不正确的信息。对于您的解决方案,我建议使用您自己的自定义块更改原始面包屑块,您将在其中显示类似产品属性的内容。
  • 感谢您的评论,但我认为这绝对是每个人都想要的想法。对不起,我没有说清楚。我只需要在面包屑中存在类别,无论哪个类别; Magento 可以自行决定。现在的问题是,我的大多数访问者甚至都看不到面包屑中的任何类别,因为它们都来自 Google 并直接登陆产品页面......它既不方便用户也不方便浏览页面。大概,直接访问时,默认会显示ID最低的类别?还是随机的?两者都可以。

标签: magento


【解决方案1】:

用这个替换“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();
}

【讨论】:

  • +1 将其保存在它所属的 Page/Block/Html/Breadcrumbs.php 中,而不是执行标准的 Magento Template Trashcan 编程,我正在从中恢复我们的网站。易于模块化。
  • 如何在不修改核心文件的情况下做到这一点?
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 不,你是对的,这不会添加到面包屑中,这是我用来复制面包屑的代码。然后,您可以创建自己的模块以在产品视图页面上显示面包屑
    • 这段代码不必要地加载了整个类别模型(并且在循环中)。有更有效的方法来解决这个问题;上面发布的代码只会增加页面加载时间。
    • @sonassi 够公平...提交您自己的答案以便我们都可以学习如何?
    • 感谢 CCBlackburn,但我对 Magento 还很陌生,所以我不知道如何使用这个 sn-p 来实现我的意图.....
    • @kavoir.com 您可以随时将其放入顶部的 view.phtml 以了解它的作用
    【解决方案4】:

    您可以为此目的使用像https://github.com/fascinosum/SeoBreadcrumbs 这样的轻量级扩展。请注意,已实现逻辑来获取分配产品的最低级别类别。您可以根据需要轻松更改此行为。

    【讨论】:

      【解决方案5】:

      找到app/code/core/Mage/Catalog/Helper/Data.php并将Data.php复制到 app/code/local/Mage/Catalog/Helper/Data.php 找到函数public function 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 1.9 上)。
      【解决方案6】:

      @jacek_podwysocki 发布了一个将 sn-p 添加到 breadcrumbs.phtml 的解决方案,它可以工作。

      代码见这里:Programmatically add breadcrumbs paths in Magento?

      只需将这个 sn-p 添加到 app/design/frontend/default/template_name/template/page/html/breadcrumbs.phtml 的顶部

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-04
        • 1970-01-01
        相关资源
        最近更新 更多