Thursday, July 4, 2013

All categories of a product show in breadcrumbs in magento


>> Here's I've shown how to show all categories of a product in breadcrumbs. It looks like :
      Home / cat1 | Home / cat1 / cat2 | Home / cat3 / Product  You can use a separator before every Home link.
  Edit the file app/code/core/Mage/Catalog/Block/Breadcrumbs.php
 Note* : You must copy the file in your local directory OR override the block before Edit.

Find the method _prepareLayout() and replace with the following code:

        protected function _prepareLayout()
        {
            if ($breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs')) {
              
                /************                Customize for product breadcrumbs            ***********/
                                              
                $product = $this->getProduct();
                $categories = $product->getCategoryIds();
                $parentArr = array();
                $childArr = array();
              
                foreach($categories as $catId){
                    if($catId != 2){
                        $category = Mage::getModel('catalog/category')->load($catId);
                      
                        if(in_array($category->getParentId(),$categories) && $category->getParentId() != 2){
                            if(!array_key_exists($category->getParentId(),$parentArr)){
                                $childArr[] = $category->getId();
                                $parentArr[$category->getParentId()] = $childArr;
                            }else{
                                if($parentArr[$category->getParentId()] == '') $childArr = array();
                                array_push($childArr,$category->getId());
                                $parentArr[$category->getParentId()] = $childArr;
                            }
                        }else{
                            $parentArr[$category->getId()] = '';
                        }
                    }
                }
              
                $i=0;
                foreach($parentArr as $pId => $cArr){
                    $cat = Mage::getModel('catalog/category')->load($pId);
                    $firstClass = ($i++ == 0) ? $cat->getId().' first' : $cat->getId();
                  
                    $breadcrumbsBlock->addCrumb('home product '.$firstClass, array('label'=>Mage::helper('core')->__('Home'),
                                                'title'=>Mage::helper('core')->__('Home Page'), 'link'=>Mage::getBaseUrl()));
                    $breadcrumbsBlock->addCrumb(strtolower(str_replace(' ','_',$cat->getName())), array('label'=> $cat->getName(),
                                                    'title'=> $cat->getName(), 'link'=> $cat->getUrl()));
                    foreach($cArr as $cId){
                        $child = Mage::getModel('catalog/category')->load($cId);
                        $breadcrumbsBlock->addCrumb('home product '.$cId.$cat->getId(), array('label'=>Mage::helper('core')->__('Home'),
                                                    'title'=>Mage::helper('core')->__('Home Page'), 'link'=>Mage::getBaseUrl()));
                        $breadcrumbsBlock->addCrumb(strtolower(str_replace(' ','_',$cat->getName())).$cId, array('label'=> $cat->getName(),
                                                        'title'=> $cat->getName(), 'link'=> $cat->getUrl()));
                        $breadcrumbsBlock->addCrumb(strtolower(str_replace(' ','_',$child->getName())), array('label'=> $child->getName(),
                                                    'title'=> $child->getName(), 'link'=> $child->getUrl()));
                    }      
                }
                if(count($parentArr) == 0){
                    $breadcrumbsBlock->addCrumb('home product first', array('label'=>Mage::helper('core')->__('Home'),
                                                'title'=>Mage::helper('core')->__('Home Page'), 'link'=>Mage::getBaseUrl()));
                }
                $breadcrumbsBlock->addCrumb('prod'.$product->getId(), array('label'=> $product->getName(), 'title'=> $product->getName(), 'link'=> ''));
              
                /***********            Customize for product breadcrumbsBlock    END            *************/
              
            }
            return parent::_prepareLayout();
        }