Monday, 6 May 2013

show All category with their thumbnail image in magento

first create a page in template/catalog/navigation
category_listing.phtml

Paste the below code:

 <?php
$_main_categories=$this->getStoreCategories();
if($_main_categories->count()):
    foreach ($_main_categories as $_main_category):
     
              ?>
                    <?php
                    $cur_category=Mage::getModel('catalog/category')->load($_main_category->getId());
                    $layer = Mage::getSingleton('catalog/layer');
                    $layer->setCurrentCategory($cur_category);
                   ?>               

                    <div class="catagory-box">
                        <div class="greenBorder">
                            <div class="img-align">
                                <div class="img-valign" align="center">
                                    <a href="<?php echo $this->getCurrentCategory()->getUrl()?>">
    <?php if($_imageUrl=$this->getCurrentCategory()->getThumbnailImageUrl()):?>
       <img src="<?php echo $_imageUrl ?>" alt="<?php echo $this->getCurrentCategory()->getName();?>" width="100" height="100">
                                    <?php else:?>
    <img src="<?php echo $this->getSkinUrl('images/add-01.gif')?>" alt="<?php echo $this->getCurrentCategory()->getName();?>">
                                    <?php endif;?>
                                    </a>
                                </div>
                            </div>
                        </div>
                        <div class="cat-box-text">
                            <span><a href="<?php echo $this->getCurrentCategory()->getUrl()?>"><?php echo $this->getCurrentCategory()->getName();?></a></span><br>
                            <?php echo $this->getCurrentCategory()->getDescription() ?>
                        </div>
                    </div>
<?php
          $_sub_categories=$_main_category->getChildren();
            foreach ($_sub_categories as $_sub_category): ?>
<?php
                    $cur_category1=Mage::getModel('catalog/category')->load($_sub_category->getId());
                    $layer = Mage::getSingleton('catalog/layer');
                    $layer->setCurrentCategory($cur_category1);
?><div class="catagory-box">
                        <div class="greenBorder">
                            <div class="img-align">
                                <div class="img-valign" align="center">
                                    <a href="<?php echo $this->getCurrentCategory()->getUrl()?>">
    <?php if($_imageUrl=$this->getCurrentCategory()->getThumbnailImageUrl()):?>
       <img src="<?php echo $_imageUrl?>" alt="<?php echo $this->getCurrentCategory()->getName();?>" width="100" height="100">
                                    <?php else:?>
    <img src="<?php echo $this->getSkinUrl('images/add-01.gif')?>" alt="<?php echo $this->getCurrentCategory()->getName();?>">
                                    <?php endif;?>
                                    </a>
                                </div>
                            </div>
                        </div>
                        <div class="cat-box-text">
                            <span><a href="<?php echo $this->getCurrentCategory()->getUrl()?>"><?php echo $this->getCurrentCategory()->getName();?></a></span><br>
                            <?php echo $this->getCurrentCategory()->getDescription() ?>
                        </div>
                    </div>
<?php
          $_sub_sub_categories=$_sub_category->getChildren();
            foreach ($_sub_sub_categories as $_sub_sub_category): ?>
<?php
                    $cur_category2=Mage::getModel('catalog/category')->load($_sub_sub_category->getId());
                    $layer = Mage::getSingleton('catalog/layer');
                    $layer->setCurrentCategory($cur_category2);
?><div class="catagory-box">
                        <div class="greenBorder">
                            <div class="img-align">
                                <div class="img-valign" align="center">
                                    <a href="<?php echo $this->getCurrentCategory()->getUrl()?>">
    <?php if($_imageUrl=$this->getCurrentCategory()->getThumbnailImageUrl()):?>
       <img src="<?php echo $_imageUrl?>" alt="<?php echo $this->getCurrentCategory()->getName();?>" width="100" height="100">
                                    <?php else:?>
    <img src="<?php echo $this->getSkinUrl('images/add-01.gif')?>" alt="<?php echo $this->getCurrentCategory()->getName();?>">
                                    <?php endif;?>
                                    </a>
                                </div>
                            </div>
                        </div>
                        <div class="cat-box-text">
                            <span><a href="<?php echo $this->getCurrentCategory()->getUrl()?>"><?php echo $this->getCurrentCategory()->getName();?></a></span><br>
                            <?php echo $this->getCurrentCategory()->getDescription() ?>
                        </div>
                    </div>
<?php
         endforeach;
            endforeach;
        endforeach;

endif;
?>



Now go to app/code/core/Mage/Catalog/Model/category.php
and add the function:

public function getThumbnailImageUrl()
   {
      $url = false;

      if ($image = $this->getThumbnail()) {

         $url = Mage::getBaseUrl('media').'catalog/category/'.$image;
      }
      return $url;
   }


Now make a cms page and call the phtml file as:
{{block type="catalog/navigation" name="catalog.categories" template="catalog/navigation/category_listing.phtml"}}



Note: This will shows upto 3-level categories in magento

No comments:

Post a Comment