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
nof8ole posted on - Tuesday 15th of April 2014 08:16:15 AM
mad posted on - Thursday 29th of May 2014 09:13:35 AM
Works fine on magento 1.8.1
sebastian bernal posted on - Tuesday 15th of July 2014 09:36:58 PM
surao posted on - Monday 18th of August 2014 08:16:01 PM
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
<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>
<?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
Harry posted on - Sunday 31st of August 2014 10:04:02 AM
Jeff posted on - Friday 21st of November 2014 12:53:58 AM
Enrique posted on - Tuesday 11th of November 2014 01:38:13 AM
Magento1.9
Thanks for that!
Pricio posted on - Monday 11th of November 2013 12:12:37 PM
Pc posted on - Tuesday 2nd of June 2015 12:04:44 AM
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
Damodar Bashyal posted on - Tuesday 13th of June 2017 12:25:44 AM
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