Magento - Force Display Full Breadcrumb Path

Posted by Damodar Bashyal on April 17, 2013

 

UPDATE: This was previous method that I used to force display full breadcrumbs for all product pages. Scroll to the bottom of the article to find new method which you can download from github as well.

Today's another issue with magento: not displaying full breadcrumb i.e. categories not included in path. I'm not overriding this time as very little time was assigned to fix this.

copy core file to local: app\code\local\Mage\Catalog\Block\Breadcrumbs.php

On "protected function _prepareLayout()" add below code just before "$title = array();"

$current_category   = Mage::registry('current_category');
$current_product    = Mage::registry('current_product');
if(!$current_category && $current_product){
	$categories = $current_product->getCategoryCollection()->addAttributeToSelect('name')->setPageSize(1);
	foreach($categories as $category) {
		Mage::unregister('current_category');
		Mage::register('current_category', $category);
	}
}

After this addition of code, you will always get full magento breadcrumb.

Full code looks like this:

/**
 * Preparing layout
 *
 * @return Mage_Catalog_Block_Breadcrumbs
 */
protected function _prepareLayout()
{
	if ($breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs')) {
		$breadcrumbsBlock->addCrumb('home', array(
			'label'=>Mage::helper('catalog')->__('Home'),
			'title'=>Mage::helper('catalog')->__('Go to Home Page'),
			'link'=>Mage::getBaseUrl()
		));
		// sometimes magento can't get category associated with a product
		// so the full breadcrumb is not shown
		// this is a hack to fix the issue.
		$current_category   = Mage::registry('current_category');
		$current_product    = Mage::registry('current_product');
		// let's check if magento knows what current category is
		// if it doesn't know, let's feed this info to it's brain :)
		if(!$current_category && $current_product){
			$categories = $current_product->getCategoryCollection()->addAttributeToSelect('name')->setPageSize(1);
			foreach($categories as $category) {
				Mage::unregister('current_category');
				Mage::register('current_category', $category);
			}
		}
		$title = array();
		$path  = Mage::helper('catalog')->getBreadcrumbPath();
		foreach ($path as $name => $breadcrumb) {
			$breadcrumbsBlock->addCrumb($name, $breadcrumb);
			$title[] = $breadcrumb['label'];
		}
		if ($headBlock = $this->getLayout()->getBlock('head')) {
			$headBlock->setTitle(join($this->getTitleSeparator(), array_reverse($title)));
		}
	}
	return parent::_prepareLayout();
}

I have now created new module based on Chris's suggestion here

- https://github.com/dbashyal/Magento_Breadcrumbs

Now you can request any update pull request directly in github :)

Nick Annies posted on - Monday 11th of November 2013 12:15:39 PM

Great stuff, this worked a treat for me on 1.7.02...thanks!

nof8ole posted on - Tuesday 15th of April 2014 08:16:15 AM

thx great useful stuff work for me -> magento 1.7.0.2

mad posted on - Thursday 29th of May 2014 09:13:35 AM

Thank you very much!
Works fine on magento 1.8.1

sebastian bernal posted on - Tuesday 15th of July 2014 09:36:58 PM

Tanks so much men

surao posted on - Monday 18th of August 2014 08:16:01 PM

I have a product A and it is assigned to 3 categories

Shirts >> full sleeves >> product A
T-Shirts >> Polo >> product A
Sweaters >> Woolen >> product A

However when I access that product is shows the breadcrumbs of category sweaters.

How do I make it always take the category of shirts?

Chris Pook posted on - Thursday 28th of August 2014 05:05:03 AM

This should be implemented as an observer rather than a rewrite or core hack.

<frontend>
<events>
<catalog_controller_product_init_after>
<observers>
<add_category_breadcrumb>
<class>custom_module/observer</class>
<method>addCategoryBreadcrumb</method>
</add_category_breadcrumb>
</observers>
</catalog_controller_product_init_after>
</events>
</frontend>


&lt;?php
class Custom_Module_Model_Observer
{
/**
* Adds category to breadcrumb for product views
* originating from outside a category, for instance
* homepage or navigation links.
*
* @return Custom_Module_Model_Observer
*/
public function addCategoryBreadcrumb(Varien_Event_Observer $observer)
{
if (!Mage::registry('current_category')) {
$product = $observer->getProduct();
$categoryIds = $product->getCategoryIds();

if(count($categoryIds)) {
$firstCategoryId = reset($categoryIds);
$category = Mage::getModel('catalog/category')->load($firstCategoryId);

if ($category->getId()) {
$product->setCategory($category);
Mage::register('current_category', $category);
}
}
}

return $this;
}
}

Damodar Bashyal posted on - Thursday 28th of August 2014 05:07:01 AM

That's awesome @ChrisPook.

Harry posted on - Sunday 31st of August 2014 10:04:02 AM

thank you, works great :-)

Jeff posted on - Friday 21st of November 2014 12:53:58 AM

worked great for me, thanks! Magento 1.7.0.2

Enrique posted on - Tuesday 11th of November 2014 01:38:13 AM

As a newbie, this was magic to me !!! Worked great on first attempt!

Magento1.9
Thanks for that!

Pricio posted on - Monday 11th of November 2013 12:12:37 PM

Thanks works fine on my magento

Pc posted on - Tuesday 2nd of June 2015 12:04:44 AM

Works great in Magento 1.7.0.2!

One small problem though: the script does not distinguish between disabled categories.

When a product is inside a disabled category AND another category which is enabled, the script might choose the disabled category (if it is first). On frontend there will be one breadcrumb missing (because that category is disabled).

Can the script be edited so it skips the disabled category and chooses the first enabled category?

Chowix posted on - Wednesday 23rd of December 2015 06:13:53 AM

on magento 1.9.2.2 not work :(

Damodar Bashyal posted on - Tuesday 13th of June 2017 12:25:44 AM

Hi Luenib,

Just pushed some updates, you can customize as your need. - https://github.com/dbashyal/Magento_Breadcrumbs

Cheers!

luenib posted on - Monday 12th of June 2017 11:44:57 PM

It doesn't work on Magento 1.14 EE, only Home, the root category, and the name of the product are displayed. Subcategories are ignored for some reason.
 
not published on website


QR Code: Magento - Force Display Full Breadcrumb Path