Monday, June 18, 2012

Magento Show Top Navigation with Thumnail

1.Add new function getThumbnailUrl in Mage\Catalog\Model\Category.php

public function getThumbnailUrl()
 {
  $url = false;
  if ($image = $this->getThumbnail()) {
   $url = Mage::getBaseUrl('media').'catalog/category/'.$image;
  }
  return $url;
 }

2.Add new function getThumbnailUrl in Mage\Catalog\Block\Navigation.php

public function getThumbnailUrl($category)
{
 return Mage::getModel('catalog/category')->load($category->getId())->getThumbnailUrl();
}

3. Use getThumbnailUrl to show thumnail in top navigation

Use as:
Open app\code\core\Mage\Catalog\Block\Navigation.php, then serach for html generate _renderCategoryMenuItemHtml method, here you can change html as

Replace $html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
with
$html[] = '<img src="'.$this->getThumbnailUrl($category).'" alt="'. $this->escapeHtml($category->getName()).'" />';

3 comments: