Set the default view mode to โ€œList / Gridโ€ in the .xml directory to view the category

I am trying to make my products display categories in list or grid mode by default.

<reference name="content"> <block type="catalog/category_view" name="category.products" template="catalog/category/view.phtml"> <block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml"> <block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml"> <block type="page/html_pager" name="product_list_toolbar_pager"/> <!-- The following code shows how to set your own pager increments --> <!-- <action method="setDefaultListPerPage"><limit>4</limit></action> <action method="setDefaultGridPerPage"><limit>9</limit></action> <action method="addPagerLimit"><mode>list</mode><limit>2</limit></action> <action method="addPagerLimit"><mode>list</mode><limit>4</limit></action> <action method="addPagerLimit"><mode>list</mode><limit>6</limit></action> <action method="addPagerLimit"><mode>list</mode><limit>8</limit></action> <action method="addPagerLimit" translate="label"><mode>list</mode><limit>all</limit><label>All</label></action> --> </block> <action method="addColumnCountLayoutDepend"><layout>empty</layout><count>6</count></action> <action method="addColumnCountLayoutDepend"><layout>one_column</layout><count>5</count></action> <action method="addColumnCountLayoutDepend"><layout>two_columns_left</layout><count>4</count></action> <action method="addColumnCountLayoutDepend"><layout>two_columns_right</layout><count>4</count></action> <action method="addColumnCountLayoutDepend"><layout>three_columns</layout><count>3</count></action> <action method="setToolbarBlockName"><name>product_list_toolbar</name></action> </block> </block> </reference> 

Here is the code that I have inside <catalog_category_layered> and <catalog_category_default> . Know anyone how to do this? I searched Google many times and did not find a solution.

+4
source share
3 answers

You can do this in the XML layout or in the "Custom layout setup" section in admin with the following xml:

 <reference name="product_list_toolbar"> <action method="setData"><key>_current_grid_mode</key><value>list</value></action> </reference> 

Verify that the toolbar block name was set in the product list block as follows:

 <action method="setToolbarBlockName"><name>product_list_toolbar</name></action> 
+11
source

You can set the grid or list from the backend.

System-> Configuration-> Catalog-> Appearance-> List Mode

+10
source

If you decide to use the method described by @ rengaw83, you will no longer be able to switch between modes in this category. For example, if you click "Grid", the mode will not be changed to grid mode.

To be able to switch modes and simply set the default view mode in a category using a custom layout, you need to override the main block of the toolbar and add the following method to it:

 /** * Sets the current View modes (grid, list, etc.) * * @param array $modes */ public function setCurrentModes($modes) { $this->_availableMode = $modes; $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 { $mode = Mage::getSingleton('catalog/session')->getDisplayMode(); } if (!$mode || !isset($this->_availableMode[$mode])) { $mode = $defaultMode; } $this->setData('_current_grid_mode', $mode); } 

Then you can configure the modes in the custom layout tab as follows:

 <reference name="product_list_toolbar"> <action method="setCurrentModes"> <modes> <list>List</list> <grid>Grid</grid> </modes> </action> </reference> 

for default list mode or

 <reference name="product_list_toolbar"> <action method="setCurrentModes"> <modes> <grid>Grid</grid> <list>List</list> </modes> </action> </reference> 

for the default grid mode. Or you can even pass only one mode to set only the grid or list mode.

+2
source

Source: https://habr.com/ru/post/1432781/


All Articles