Extended breadcrumbs

This time we describe the breadcrumbs that redirect you to a product page. We think that almost every one of you had a chance to work with the breadcrumbs in Magento.

If so, you should know about the product breadcrumbs type:

Typical product's breadcrumb

Let’s find out how to receive these breadcrumbs – of course, you will see them after redirecting from the search page to the product page. But pay attention that when you go from the product category page to the product page – the breadcrumbs have completely different look:

Correct product's breadcrumb

In the second case, it shows the way how the product can be found in the store. So, why Magento is building the products breadcrumbs differently? This answer you can find in the Magento core file:

app\code\core\Mage\Catalog\Block\Breadcrumbs.php

which is used to generate the breadcrumbs of the website pages.

Moreover, pay attention to the function getBreadcrumbPath(), which is declared here:

app\code\core\Mage\Catalog\Helper\Data.php

When you look into its code:

public function getBreadcrumbPath()
{
  if (!$this->_categoryPath) {

    $path = array();
    if ($category = $this->getCategory()) {
        $pathInStore = $category->getPathInStore();
        $pathIds = array_reverse(explode(',', $pathInStore));

        $categories = $category->getParentCategories();

        // add category path breadcrumb
        foreach ($pathIds as $categoryId) {
            if (isset($categories[$categoryId]) && $categories[$categoryId]->getName()) {
                $path['category'.$categoryId] = array(
                    'label' => $categories[$categoryId]->getName(),
                    'link' => $this->_isCategoryLink($categoryId) ? $categories[$categoryId]->getUrl() : ''
                );
            }
        }
    }

    if ($this->getProduct()) {
        $path['product'] = array('label'=>$this->getProduct()->getName());
    }

    $this->_categoryPath = $path;
  }
  
  return $this->_categoryPath;
}

You will see that the full product breadcrumbs can be shown only if we have information about the category where this product is placed. As follows, we receive:

Home / Test Category / Test SubCategory / Some product

when we have information about the category. And in case we do not know the category, we will only have:

Home / Some product

Note that it is also possible to output the full product breadcrumbs regardless the page which you get to it from – and to do this, you only need to define the product category when it is not specified. Moreover, the information about the assigned products to the categories you can find in the table:

catalog_category_product

It is worth mentioning that easier way to get the information about product’s categories is just to use the following code:

$product_categories = Mage::registry('current_product')->getCategoryCollection()->exportToArray();

if (count($product_categories)) {
    foreach( $product_categories as $category_id => $category_data ) {
        print_r($category_data); 
    }
}

If you are already good at Magento, we are sure that you can add the needed changes by yourself. But to simplify this process, we’ve created the module which you can download and set to work with the breadcrumbs. This module allows to display a full path from the categories to a product on the product page.

We hope these simple tips will be useful for you, and you will not have any difficulties with breadcrumbs.

Read more: