Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically add breadcrumbs paths in Magento?

Tags:

magento

In Magento, when the user directly accesses the product page, such as from Google, the breadcrumbs would only be "Home" -> "Product Name".

How can I add categories in there even when users access the page directly from Google?

For example, on this page, I want to add the categories "Wedding Apparel" and "Wedding Dresses" in the breadcrumbs. I came up with an idea other than hard editing breadcrumbs.phtml but is there any way I can programmatically add a breadcrumbs item in template/catalog/product/view.phtml ?

I can get the categories (title and link) of the current product and then use some function / method to add them in the breadcrumbs dynamically and programmatically. Is this possible?

like image 748
datasn.io Avatar asked Jul 11 '26 18:07

datasn.io


1 Answers

Here's the code that forces Magento to display the complete breadcrumb, including categories by looping trough each category for the current product:

© Danny Vince

<?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
        );
    }
}
?>
like image 52
jacek_podwysocki Avatar answered Jul 13 '26 10:07

jacek_podwysocki