Extended breadcrumbs Extended breadcrumbs

If you ever had questions about the breadcrumbs in Magento, how you can use them, and how they can redirect you to a product page, atwix.com has developed an easy-to-understand tutorial on how breadcrumbs can work for you, answering all these questions.

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:

1
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:

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

When you look into its code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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:

1
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:

1
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:

1
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:

1
2
3
4
5
6
7
$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.

Did you find this article useful?

Source: atwix.com/magento

No comments so far.

Be first to leave comment below.

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