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

时间:2012-09-14 02:51:51

标签: magento

在Magento,我们现在所拥有的是个别产品页面上的面包屑总是根据用户到达页面的方式而改变。例如,如果用户从顶级类别到子类别再到产品一直点击,则面包屑将类似于“主页&gt;&gt;顶级类别&gt;&gt;子类别&gt;&gt;产品< /强>”。

但是,如果用户直接到达产品页面(例如Google查询),则面包屑只会是“主页&gt;&gt;产品”。像这样是不酷的。

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

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

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

任何帮助将不胜感激!

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

编辑:总之,无论用户如何到达产品页面,我只希望在产品页面的面包屑中显示类别。只要类别存在,哪个类别无关紧要。

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

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

6 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:3)

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

答案 2 :(得分: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().'/';
}

答案 3 :(得分:2)

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

答案 4 :(得分:1)

查找app / code / core / Mage / Catalog / Helper / Data.php并将Data.php复制到 应用程序/代码/本地/法师/目录/助手/ Data.php 找到函数public function getBreadcrumbPath(),将以下代码添加到此函数的开头

$(document).ready(function() {
   var musics = $('.player');

   $('.play').click(function () {
     var thisMusic = musics[0];
     if (thisMusic.paused) {

          thisMusic.play();

          $('.timeline').click(function (event) {

                var duration = thisMusic.duration;    
                var clickPercent = ((event.pageX - $(this).offset().left) / $(this).width());    

                console.log(parseInt(parseFloat(duration) * parseFloat(clickPercent)));  // Working.

                thisMusic.currentTime = parseInt(parseFloat(duration) * parseFloat(clickPercent));   // Not working, doesn't set.
           });

     } else {

            thisMusic.pause();

            $('.timeline').unbind('click');
    }

答案 5 :(得分:0)

@ jacek_podwysocki发布了一个解决方案,可以将一个代码段添加到breadcrumbs.phtml中,然后就可以了。

请在此处查看代码:Programmatically add breadcrumbs paths in Magento?

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

的顶部

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