Friday, 14 June 2013

Add subcategory thumbnail image in topmenu in magento

link :http://source.9nit.com/magento/how-to-retrieve-the-parent-category-images-in-topmenu-in-magento-42154.html

step 1:
modified mage/catalog/model/observer.php file  modify  _addCategoriesToMenu function in modify
$categoryData = array( 
like
 $categoryData = array(
                'name' => $category->getName(),
            'id' => $nodeId,
            'url' => Mage::helper('catalog/category')->getCategoryUrl($category),
            'is_active' => $this->_isActiveMenuCategory($category),
            'links' => $category->getData('links'),
            'image' => $category->getImageUrl('image'),
            'thumbnail' =>Mage::getModel('catalog/category')->load($cat)->getThumbnail(),
            'getLevel' => $category->getLevel()
            );


also add $cat = $category->getId();  after $nodeId



step2:
modify Mage/page/block/html/topmenu.php  modified _getHtml function
add below code after $html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>'
                . $this->escapeHtml($child->getName()) . '</span></a>';

// new code starts here 

$parentLevels = $child->getLevel();
            if($parentLevels != 0)
              {
                //echo $child->getData('thumbnail');

                   $urls = Mage::getBaseUrl('media').'catalog/category/'.$child->getData('thumbnail');
                if(!file_exists("./media/catalog/category/resized"))    
                mkdir("./media/catalog/category/resized",0777);
                $imageName = substr(strrchr($urls,"/"),1);
                $imageResized = Mage::getBaseDir

('media').DS."catalog".DS."category".DS."resized".DS.$imageName;
                $dirImg = Mage::getBaseDir().str_replace("/",DS,strstr($urls,'/media'));
                if (!file_exists($imageResized)&&file_exists($dirImg)) :
                     $imageObj = new Varien_Image($dirImg);
                      $imageObj->constrainOnly(TRUE);
                      $imageObj->keepAspectRatio(FALSE);
                     $imageObj->keepFrame(TRUE);
                     $imageObj->resize(28,28);
                     $imageObj->save($imageResized);
               endif;

              $newImageUrl = Mage::getBaseUrl('media')."catalog/category/resized/".$imageName;

                     if($child->getData('thumbnail') !="")
                    {
                            $html .= '<img src="'.$newImageUrl.'"   />';
                     }
                }



2 comments:

  1. Hi,

    Can you please explain how to put the icon/thumb before the text in the same line, in my Magento it gets after the text in the line above.
    Thanks a lot it will be a great help if you give me 2 mins of your time.

    Regards.

    ReplyDelete