Wednesday, 25 September 2013

Show category name in breadcrumb in magento

Show category name in breadcrumb in magento when click on home page product

Just add the below code in top of the breadcrumbs.phtml page :-

<?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
        );
    }
}

?>

You are done!!!!!

2 comments: