How to force Magento into showing full breadcrumbs (Now with Google Rich Snippets)

This is a mod to force Magento into showing the full breadcrumbs page structure. Which helps for easy navigation and most importantly for search engine spidering.

  • Get your breadcrumbs.phtml file from public_html/app/design/frontend/default/YOUR-TEMPLATE/template/page/html/
  • Add this code to the top of the code:
<?php
if ($product = Mage::registry('current_product')) {
$categories = $product->getCategoryCollection()->load();

if($categories) {
foreach ($categories as $category)
{
if($category) {
$category = Mage::getModel('catalog/category')->load($category->getId());
break;
}
}
}
$lastCrumbName = $product->getName();
$lastCategoryAdjust = 0;
}
else {
if($category = Mage::registry('current_category')) {
$lastCrumbName = $category->getName();
}
$lastCategoryAdjust = 1;
}

if($category) {
if($path = $category->getPath()) {
$path = explode('/', $path);
$crumbs = array('home' => array('label' => 'Home',
'title' => 'Home',
'link' => Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB),
'first' => true,
'last' => false
));
for($i = 2; $i < count($path) - $lastCategoryAdjust; $i++) {
$cur_category = Mage::getModel('catalog/category')->load($path[$i]);
if($cur_category && $cur_category->getIsActive()) {
$crumbs['category' . $path[$i]] = array('label' => $cur_category->getName(),
'title' => $cur_category->getName(),
'link' => $cur_category->getUrl(),
'first' => false,
'last' => false
);
}
}
$crumbs['current'] = array('label' => $lastCrumbName,
'title' => '',
'link' => '',
'first' => false,
'last' => true
);
}
}
?>

So that you end up with something like this:

<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE_AFL.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category design
* @package base_default
* @copyright Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
?>

<?php
if ($product = Mage::registry('current_product')) {
$categories = $product->getCategoryCollection()->load();

if($categories) {
foreach ($categories as $category)
{
if($category) {
$category = Mage::getModel('catalog/category')->load($category->getId());
break;
}
}
}
$lastCrumbName = $product->getName();
$lastCategoryAdjust = 0;
}
else {
if($category = Mage::registry('current_category')) {
$lastCrumbName = $category->getName();
}
$lastCategoryAdjust = 1;
}

if($category) {
if($path = $category->getPath()) {
$path = explode('/', $path);
$crumbs = array('home' => array('label' => 'Home',
'title' => 'Home',
'link' => Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB),
'first' => true,
'last' => false
));
for($i = 2; $i < count($path) - $lastCategoryAdjust; $i++) {
$cur_category = Mage::getModel('catalog/category')->load($path[$i]);
if($cur_category && $cur_category->getIsActive()) {
$crumbs['category' . $path[$i]] = array('label' => $cur_category->getName(),
'title' => $cur_category->getName(),
'link' => $cur_category->getUrl(),
'first' => false,
'last' => false
);
}
}
$crumbs['current'] = array('label' => $lastCrumbName,
'title' => '',
'link' => '',
'first' => false,
'last' => true
);
}
}
?>

<?php if($crumbs && is_array($crumbs)): ?>
<div class="breadcrumbs">
<ul>
<?php foreach($crumbs as $_crumbName=>$_crumbInfo): ?>
<li class="<?php echo $_crumbName ?>">
<?php if($_crumbInfo['link']): ?>
<a href="<?php echo $_crumbInfo['link'] ?>" title="<?php echo $this->htmlEscape($_crumbInfo['title']) ?>"><?php echo $this->htmlEscape($_crumbInfo['label']) ?></a>
<?php elseif($_crumbInfo['last']): ?>
<strong><?php echo $this->htmlEscape($_crumbInfo['label']) ?></strong>
<?php else: ?>
<?php echo $this->htmlEscape($_crumbInfo['label']) ?>
<?php endif; ?>
<?php if(!$_crumbInfo['last']): ?>
<span> > </span>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>

Update 29/05/2013

If you want to combine the above with Google Rich Snippets, i.e. so that the breadcrumbs show up as Rich Snippets and you are still forcing Magento into showing full breadcrumbs, then use this code instead:

<?php
if ($product = Mage::registry('current_product')) {
$categories = $product->getCategoryCollection()->load();

if($categories) {
foreach ($categories as $category)
{
if($category) {
$category = Mage::getModel('catalog/category')->load($category->getId());
break;
}
}
}
$lastCrumbName = $product->getName();
$lastCategoryAdjust = 0;
}
else {
if($category = Mage::registry('current_category')) {
$lastCrumbName = $category->getName();
}
$lastCategoryAdjust = 1;
}

if($category) {
if($path = $category->getPath()) {
$path = explode('/', $path);
$crumbs = array('home' => array('label' => 'Home',
'title' => 'Home',
'link' => Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB),
'first' => true,
'last' => false
));
for($i = 2; $i < count($path) - $lastCategoryAdjust; $i++) {
$cur_category = Mage::getModel('catalog/category')->load($path[$i]);
if($cur_category && $cur_category->getIsActive()) {
$crumbs['category' . $path[$i]] = array('label' => $cur_category->getName(),
'title' => $cur_category->getName(),
'link' => $cur_category->getUrl(),
'first' => false,
'last' => false
);
}
}
$crumbs['current'] = array('label' => $lastCrumbName,
'title' => '',
'link' => '',
'first' => false,
'last' => true
);
}
}
?>

<?php if($crumbs && is_array($crumbs)): ?>
<div class="breadcrumbs">
<ul xmlns:v="http://rdf.data-vocabulary.org/#">
<?php foreach($crumbs as $_crumbName=>$_crumbInfo): ?>
<li class="<?php echo $_crumbName ?>" typeof="v:Breadcrumb">
<?php if($_crumbInfo['link']): ?>
<a href="<?php echo $_crumbInfo['link'] ?>" title="<?php echo $this->htmlEscape($_crumbInfo['title']) ?>" rel="v:url" property="v:title"><?php echo $this->htmlEscape($_crumbInfo['label']) ?></a>
<?php elseif($_crumbInfo['last']): ?>
<strong><?php echo $this->htmlEscape($_crumbInfo['label']) ?></strong>
<?php else: ?>
<span rel="v:child" property="v:title"><strong><?php echo $this->htmlEscape($_crumbInfo['label']) . ' for sale' ?></strong></span>
<?php endif; ?>
<?php if(!$_crumbInfo['last']): ?>
<span> > </span>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>

To test the richsnippets are working use Google Testing Tool: http://www.google.com/webmasters/tools/richsnippets

If you want to use all the other currently available rich snippets the plugin I recommend is: http://grafischdirect.com/magento-extension-google-rich-snippets (€79.96) as a combination of the above code and this plugin will give you complete Rich Snippet coverage. FYI at the time of writing this Google isn’t showing product images in RS but it won’t be long… In any event the Grafisch Direct plugin has already got the code to included for when the images when they become available.

26 comments
  1. That is really great!
    Dose it work if the product belongs to both of rootcategory and subcategory?

  2. I appreciate your reply, Danny.
    I do understand. My root cat has lower ID no. than sub cat.
    That is why sub cat disappeared from breadcrumbs on my product page.
    Is there any solutions for it at this moment??
    If any, please kindly let me know.

  3. Hello,

    Yes, this code is fantastic!
    The only issue I just found is that before with the original file when you clicked on product reviews the breadcrumbs would end in / Product Reviews and now mine doesn’t.

    I removed the code and it shows up again. But then of course the full category trail does not.

    Would there be any fix for this?
    Thank you!

  4. I have tried this, but it doesn’t appear to be looping. I only get 1 breadcrumb line, and it has the first selected category (based on my category order).

    Any thoughts?

    Thanks

  5. I used this code on a multi-store setup. But for some reason it only works on the store using the first root category. Other stores that use different root categories do not show breadcrumbs. Is there a way this could be edited to use different root categories?

  6. Nothing affected. Went perfectly. Thank you so much for the Google rich snippets code. Have been searching for this code for some time now but only run into errors . Either the links wouldn’t work, or couldn’t get rid of the Home section.

  7. Can you tell me if the breadcrumbs (Now with Google Rich Snippets) will show the image of the product or is there additional code needed? Thanks.

  8. Hello, good article, however i have noticed that after I have copied your code, on the cms page the breadcrumbs are wrong. Instead of displaying ” Home + title’s page” now I have “Home > Default category”.
    Is there a way to correct it?
    Thank you
    Andrea

  9. Hi Dan,

    I have a problem with the Bread crumb not showing the entire trail on our product page of our website. I have implemented your code and it doesn’t work.

    Is there anything else I can do? I would appreciate any other tips.

    Regards

    Brendon

  10. On product pages it does not show sub-categories… One more magento-snippet / tutorial which doesn’t work from scratch, that seems to be cool because everybody does it …

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

Hiding Empty Widgets in Magento

Next Post

10 WordPress Plugins You Can’t Live Without

Related Posts