Results 1 to 2 of 2

Thread: Add categories in breadcrumbs on product pages in Magento

  1. #1
    Junior Member Princessly's Avatar
    Join Date
    Oct 2012
    Location
    Princessly.com
    Posts
    29

    Add categories in breadcrumbs on product pages in Magento

    After some asking around and testing, I finally got categories added in the breadcrumbs of product pages in Magento even when visitors arrive directly from Google.

    Just add this snippet to the top of app/design/frontend/your/template/template/page/html/breadcrumbs.phtml (change your/template to your own template path)

    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
            );
        }
    }
    ?>
    Live in the fairy tale, Princessly, the valiant store.

  2. #2
    Administrator Kavoir's Avatar
    Join Date
    Dec 2009
    Posts
    183
    Really nice thanks! This is way much better than the default path for individual product pages. It would definitely increase PV and give users a sense of structure for the site.
    The road goes ever on and on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •