Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set "List / Grid" default view mode in catalog.xml for category view

Tags:

magento

I'm trying to do my category view display products in list or grid mode as 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 in <catalog_category_default>. Know anyone how to do it? I was googling to much time and didn't found a solution.

like image 376
Carlos Avatar asked Sep 06 '12 17:09

Carlos


3 Answers

You can do this in the layout XML or the "Custom Layout Update" 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>

Be sure, the toolbar block name was set in the product list block like this:

<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
like image 97
rengaw83 Avatar answered Jan 03 '23 16:01

rengaw83


You can set grid or list from backend

System->Configuration->Catalog->Frontend->List mode

like image 36
blakcaps Avatar answered Jan 03 '23 14:01

blakcaps


If you choose to use the method described by @rengaw83, you will not be able to switch between modes in that category anymore. For instance, if you click on "Grid", the mode will not change to grid mode.

To be able to switch modes and just set the default view mode in a category via a custom layout, you need to override the core Toolbar block, 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 will be able to set modes in custom layout tab like that:

<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 default grid mode. Or you even can pass only one mode to set only grid or list mode available.

like image 44
Andrew Stepanchuk Avatar answered Jan 03 '23 14:01

Andrew Stepanchuk