Thursday 11 October 2012

Magento: How to list categories in sidebar in Magento site

Magento: How to list categories in sidebar in Magento site.

Let me help you to create a module to list categories in sidebar or where ever wanted. This is a very simple Module.

Step 1:
Namespace: Dinesh
Module Name: Sidebarcategory

First we need to create a xml inside modules folder to get out module active inside magento

create a Dinesh_Sidebarcategory.xml in the following path "app\etc\modules\Dinesh_Sidebarcategory.xml" and add the conetent below.

---------------------------------------------------------------------------------------------------------------------
<?xml version="1.0"?>
<!--
/**
 * @Sidebarcategory   Dinesh
 * @package    Dinesh_Sidebarcategory
 * @author     Dinesh
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
 -->
<config>
    <modules>
        <Dinesh_Sidebarcategory>
            <active>true</active>
            <codePool>local</codePool>
        </Dinesh_Sidebarcategory>
    </modules>
</config>

---------------------------------------------------------------------------------------------------------------------------
Step 2:

Now lets create a config.xml file in the following path "app\code\local\Dinesh\Sidebarcategory\etc\config.xml" and add the code below

----------------------------------------------------------------------------------------------------------------
<?xml version="1.0"?>
<!--
/**
 * @Sidebarcategory   Dinesh
 * @package    Dinesh_Sidebarcategory
 * @author     Dinesh
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
 -->
<config>
    <modules>
        <Dinesh_Sidebarcategory>
            <version>0.1.0</version>
        </Dinesh_Sidebarcategory>
    </modules>

    <global>
        <models>
            <sidebarcategory>
                <class>Dinesh_Sidebarcategory_Model</class>
           </sidebarcategory>
          </models>
        <blocks>
            <sidebarcategory>
                <class>Dinesh_Sidebarcategory_Block</class>
            </sidebarcategory>
        </blocks>
      </global>
</config>

------------------------------------------------------------------------------------------------------------
Step 3:

We need to create a model file, where we can write some function. Create Sidebarcategory.php under path "app\code\local\Dinesh\Sidebarcategory\Model\Sidebarcategory.php" and add below code.
-----------------------------------------------------------------------------------------------------------

<?php
class Dinesh_Sidebarcategory_Model_Sidebarcategory extends Mage_Core_Model_Abstract
{
  
  
    public function getSubCategories($cat_id, $depth)
      {
        $menu=$this->getSubCategory($cat_id, $depth);
        $this->setData('menu',$menu);
        return $this;
    }
  
  
    public function getSubCategory($cat_id, $depth)
      {  
        $root_category = Mage::getModel('catalog/category')->load($cat_id);
        $subcategories = $root_category->getChildren();
        $current_cat_id = Mage::getModel('catalog/category')->load(Mage::getSingleton('catalog/layer')->getCurrentCategory()->getId());
        $active=explode('/',$current_cat_id->getPath());
        if(strlen($subcategories)>0)
        {
            $menu.='<ul>';  
            foreach(explode(',',$subcategories) as $subcategory)
            {
                $category = Mage::getModel('catalog/category')->load($subcategory);
                if($category->getLevel() <= $depth || $depth==0)
                {
                    if(in_array($category->getId(), $active))
                    {$activeval='active';}else {$activeval='';}
                    $menu.= '<li class="level'.$category->getLevel().' '.$activeval.'" id="cat'.$category->getId().'">';
                    $menu.= '<a href="'.$category->getURL() .'" />'.$category->getName().'</a>';
                    $menu.=$this->getSubCategory($subcategory,$depth);
                    $menu.= '</li>';
                }
              
             }
             $menu.='</ul>';
         }
      
        return $menu;
  }
 
}

--------------------------------------------------------------------------------------------------------------

Step 4:

Create a block file  Sidebarcategory.php under the path "app\code\local\Dinesh\Sidebarcategory\Block\Sidebarcategory.php" and add the below code.

------------------------------------------------------------------------------------------------------------------------------------

 <?php
class Dinesh_Sidebarcategory_Block_Sidebarcategory extends Mage_Core_Block_Template
{
  
}

------------------------------------------------------------------------------------------------------------------------------------

Step 5:
Now we need to create a template file to display the category list. Create categorylist.phtml under path "app\design\frontend\default\default\template\sidebarcategory\categorylist.phtml " and add the following code below

NOTE: I have added the css part in phtml file itself, please donot do this, add to your css file and add design/css as per the design your using.
--------------------------------------------------------------------------------------------------------------

<?php
echo $this->getData('depth');
    if($this->getData('catid'))
    $root_category_id = $this->getData('catid');
    else
    echo $root_category_id= Mage::app()->getStore()->getRootCategoryId();
    if($this->getData('depth'))
    $depth=$this->getData('depth');
    else
    $depth=4;
    echo $depth;
    echo '<br/>'.$root_category_id;
    $cat=Mage::getModel('sidebarcategory/sidebarcategory')->getSubCategories($root_category_id,$depth);
    echo $cat->getData('menu');

?>
<style type="text/css">
ul,li{
margin-left:10px;
}
</style>

--------------------------------------------------------------------------------------------------------------
Step 6:

Now we have successfully create the module. In step 6 we need to call the module from a static block. and assign to a widget and display it where every needed(rightbar/leftbar/footer/anywhere..).

Login to admin 

cms->staticblock add new block.

Block Title: Left category listing

Identifier : left_category_listing

Status: enable

Content 

 {{block type="sidebarcategory/sidebarcategory" depth=3  template="sidebarcategory/categorylist.phtml"}}


Save block

Note:
 Here depth is the variable used to define the category levels.
depth=0 list all levels of category(cat->subcat->subsubcat->..............)
depth=3 list only(main category)
depth=4 lists(main category->sub category).
and so.. on...
Donot use depth=1 or 2 . they belong to the rootcategory.


Optional Param .
{{block type="sidebarcategory/sidebarcategory" depth=3  catid=22 template="sidebarcategory/categorylist.phtml"}}

catid=22 (provide the category id and it will display only the category and its subcategory for the given id , based on depth value)

if the param is not specified it will list category of store rootcategory.

--------------------------------------------------------------------------------------------------------------
Step 7:
Now we are going to create a widget and assign a static block to it.

In admin Area

Cms->Widgets

Add new widget instance

Type: Cms static block

Design: your current  theme package i have used the default package (default/default).

Assign to Store Views : All store views

 Layout Updates

Display On: All pages

Block Reference: Left column

Widget properties.

Block: select the static block we created.

Save Widget.

That's it we have complete our module call, now just go to your front and refresh to see the category listed in the leftcolum of your theme. note that your on the page that contain leftcolumn

Thanks for reading it.









2 comments:

  1. I can't see nothing, no error but nothing is change in the layout.

    ReplyDelete