Setting number of columns on Magento search results

May 12, 2011

I have been recently working on a Magento template that required plenty of changes to the product lists and grids and some way down the line I realised that the search results weren’t playing by the rules.

In the catalogsearch layout file an action called addColumnCountLayoutDepend to set the number of columns depending on the template that is being used. Obviously you want to show more columns in the search results if you have the space to do so. This all looks great except it doesn’t seem to work. The reason I spotted this was that I changed the search results page so that I was using a two column template instead of a three column template and the columns in the product grid stayed the same. I thought, perhaps it was still using the addColumnCountLayoutDepend setting, so I changed the column number for the three column layout but again no change.

Here is the code from catalogsearch that looks like it should be dealing with this.

<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>

This all seems to work using the same process as the normal product grids that are displayed on category pages – so I thought I would take a look at how it is done there, in the catalog layout file.

It seems the catalog layout file has a default action called setColumnCount that is used. The XML still contains the addColumnCountLayoutDepend actions but this all seems to indicate that it doesn’t work. I guess it may be left over from a code change that was never finished, or maybe it is a peek at a feature we may get in a later version. Anyway, the fix is to add the setColumnCount action to the catalogsearch file, as follows.

<action method="setColumnCount"><columns>4</columns></action>
<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>

I hope that helps people out, it had me scratching my head for a while!