Thursday 4 September 2014

By default every Magento product list page has to modes of view. They are List and Grid. We can set default mode through admin. For this , we need to go to

System  >  Configuration  >  Catalog  >  Frontend  >  List mode
and set default option there. However what about if we need to show different categories in different modes ? Obviously every one think to do this programmatically. But it's somewhat handy. So I decided to drop out programmatic option. So the last option remains there is layout updation. It looks indeed awesome. It will work like a charm.

In Detail

Let us take an example. Suppose we have three categories. They are category 1, category 2 and category 3. We need to show category 2 in grid mode and rest of the categories in list mode.

For this, first create local.xml file.

File: app\design\frontend\[your_package]\[your_theme]\layout\local.xml

 
  
   
    _current_grid_modegrid
   
  
  
   
    _current_grid_modelist
   
  
  
   
    _current_grid_modelist
   
  
 
We are done. Now remove the cache and then load the categories. You see the magic, don't you ? :)

Code in Detail

First I need to tell you about local.xml file. It is a special layout udpation file which will process by Magento after processing every other layout update XML files. Hence this layout updation file is more powerful in literal sense. For an example, for quick layout changes or updations, we can obviously use local.xml file.

product_list_toolbar block is the focused block here. This block holds management of product list modes. This block is defined in app\code\core\Mage\Catalog\Block\Product\List\Toolbar.php. I need your attention on Mage_Catalog_Block_Product_List_Toolbar::getCurrentMode() method. It looks like this

    public function getCurrentMode()
    {
        $mode = $this->_getData('_current_grid_mode');
        if ($mode) {
            return $mode;
        }
        $modes = array_keys($this->_availableMode);
        $defaultMode = current($modes);
        $mode = $this->getRequest()->getParam($this->getModeVarName());
        if ($mode) {
            if ($mode == $defaultMode) {
                Mage::getSingleton('catalog/session')->unsDisplayMode();
            } else {
                $this->_memorizeParam('display_mode', $mode);
            }
        } else {
            $mode = Mage::getSingleton('catalog/session')->getDisplayMode();
        }

        if (!$mode || !isset($this->_availableMode[$mode])) {
            $mode = $defaultMode;
        }
        $this->setData('_current_grid_mode', $mode);
        return $mode;
    }
This function's job is to return the current product list mode. But please note the line just above the return statement. ie

    $this->setData('_current_grid_mode', $mode);
This reveals the fact that, the property _current_grid_mode actually holds the mode information. So this is what we are doing through your layout update. ie

 
  _current_grid_modegrid
 

Note 1:
When I tried

    grid
it didn't work. It reveals the fact that, we cannot use the magic methods of Magento through layout update. Because.. you know experience is the greatest teacher in the world :)

Thanks for go through this. I will catch you later :)