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_mode grid _current_grid_mode list _current_grid_mode list
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_mode grid
Note 1:
When I triedgrid
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 :)
Subscribe to:
Posts (Atom)
Stack Exchange
Recently Answered
Popular Posts
-
In this post, I would like to show you how can we get product option values. For this, let us start with creating a sample product that h...
-
Static blocks are very useful tools in Magento. We can effectively use them in different situations. They provide us enough power on content...
-
Some times, it would be great if we can add a collapse/expand functionality in CMS Pages in Magento. A perfect example would be a read more ...
-
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 ...
-
Here in this tutorial, I would like to share you how can we set page size for a particular category. The Page Size is actually a property...
-
The main reason, in fact the inspiration behind this blog is this THREAD . In this thread, the user want to add layout that render by...
-
Introduction ---------------------- When I started to do Module Development in Magento, a lot of time I got an error like this Fat...
Blog Archive
Categories
Powered by Blogger.