11
votes

In Magento, what we have now is that the breadcrumbs on individual product pages are always changing according to how the user has arrived at the page. For example, if user clicked all the way from top category to sub category and then to the product, the breadcrumbs would be something like "Home >> Top category >> Sub category >> Product".

However if the user arrives at the product page directly, such as from Google queries, the breadcrumbs would simply be "Home >> Product". And it is uncool to be like this.

It would definitely be a user experience improvement by adding categories in the breadcrumbs even when the user arrives at the page from Google. And it would definitely increase PV per visit because users will likely to click on parent categories in the breadcrumbs.

So is there any way to make it consistent and always show the categories path on product pages?

My idea is to edit page/html/breadcrumbs.phtml but I don't know how to add categories to the breadcrumbs only on product pages.

Any help would be appreciated!

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

Edit: In a word, I just want categories to appear in the breadcrumbs on product pages no matter how the user arrives on the product page. Doesn't matter which categories, as long as the categories are there.

Anyone got any idea? Is this that hard?

Off the top of my head, I will need to edit the breadcrumbs template and inject the categories in it if there aren't any and it's the product page....but I don't know how and I can't seem to find any relevant solutions to this anywhere...

6
I don't think your idea is something everyone will want. A lot of sites have same product under different categories, so using your logic the breadcrumbs will give incorrect info. For your solution I would recommend to change original Breadcrumbs block with your own custom block, where you'll display something like product attribute.Slayer Birden
Thanks for the comment but I think it's definitely an idea everyone wants. Sorry I didn't make it clear. I just need categories to be there, in the breadcrumbs, doesn't matter which categories; Magento can decide that for itself. The problem now is that most of my visitors don't even see any categories in the breadcrumbs because they all arrive from Google and land directly on the product pages.....it's neither user-friendly nor page-view-friendly. Probably, when accessed directly, categories with lowest ID would be displayed by default? Or randomly? Either are fine.datasn.io

6 Answers

16
votes

Replace _toHtml() function in "app/code/core/Mage/Page/Block/Html/Breadcrumbs.php" with this one.

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

You can rewrite product model to alter getProductUrl method. Basically get the first category of that product and append category path before product path.

2
votes

I completely understand what you are wanting to do, I have noticed that even a product linked from Magento's own search will lose it's category breadcrumbs. I agree with Slayer, I would implement your own custom block for product pages only.

You can retrieve the products category tree using the following code. I have tested this (in Magento CE 1.7.0.2) with products that only have one category hierarchy and those that have multiple.

NOTE: You'll probably want to do some work on this code, as it's loading each category object into an array, this could become quite memory intensive. I would recommend adding only the information required into an associative array and then adding that array to the main array

$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().'/';
}
2
votes

You can use for this purposes a light extension like https://github.com/fascinosum/SeoBreadcrumbs. Please be aware there has been implemented logic to get the lowest level category on which product is assigned. You can easily change this behavior according to your needs.

1
votes

Find app/code/core/Mage/Catalog/Helper/Data.php and copy Data.php to app/code/local/Mage/Catalog/Helper/Data.php Find the function public function getBreadcrumbPath(), add following code to the start of this function

    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
votes

@jacek_podwysocki posted a solution to add a snippet to breadcrumbs.phtml and it works.

See here for the code: Programmatically add breadcrumbs paths in Magento?

Just add this snippet to the top of app/design/frontend/default/template_name/template/page/html/breadcrumbs.phtml

The added page load / PHP rendering time is only 0.05 second according to microtime().