
Published by on January 6th, 2010 7 Comments »
Task: change the number of columns and displayed products on individual category pages.
Magento uses a combination of xml layout directives and php/html (.phtml) template files to build the front-end interface (storefront) of a web shop. Fundamental changes to the look and feel of the store can and should be made directly in these files, all of which reside in the directory:
/app/design/frontend/default/
Smaller scale tweaks to page content can also be added from within the admin interface. Customising the content of an individual category page, for example, is achieved through adding xml code segments to the Custom Design tab of a category.
The difficulty we found was that any customisations for an individual category were added to the page in addition to the standard category layout definitions, rather than replace them with a new, unique set of directives. Resulting, in our case, to duplicated elements on the page.
The admin allows selection of an entirely different template for an individual category. A powerful option, that’s for sure, but hugely impractical for ongoing maintenance. Multiple templates means multiple changes to layout, phtml and possibly css files if any tweaks are required in the future.
The default category layouts are defined in /app/design/frontend/default/-your-template-/layout/catelog.xml (just look for the <catalog_category_default> element).
To add code for a specific category, all one needs to do is create a new element containing the internal ID of that category (changing XX to the category’s internal ID):
<CATEGORY_XX>
....
</CATEGORY_XX>
A good solution which fails only in that it becomes less portable. These xml files are designed to handle the template as a whole. Having specific category designations here mean the template can only be used for a single store with the correct category ID values.
The most practical solution, without a doubt. Here the custom code affects only the single category (and its child-categories should that be selected as an option).
However, both Solution 2 and Solution 3 must solve the problem that additional xml is added to the default xml. In our case, however, we must override the default settings to avoid duplication.
Thankfully, Magento provides a means of overriding the default elements, as defined in the layout template, to be replaced by new elements. You can see the unsetChild methods implimented in the screenshot here.
The method tells magento to remove whatever definitions that have already been created for the named elements. Usually, these would have been defined in the main template xml files. It does not remove the named elements themselves, so you are then free to redefine those elements as you wish.
In our case, we wanted to replace the category.products, product_list, and product_list_toolbar elements to avoid duplicating the product grid.
Once unsetChild had been called, we were free to add our customised grid definition below it.
The results are a default, 3-column, 9 product grid for most of the catalog, but a 4-column, 12 product grid for some individual category pages.
Magento also provides the ability to entirely remove elements from the xml definition.
<remove name="xxxx" />
This completely removes the defined element, by name. Inappropriate in our case, because it does not provide the option of re-inserting a new version of the element into the page; it removes that element name entirely.
COMMENT APPROVAL POLICY: Please use a genuine name and email address for your comment. Please use your real name, not SEO keyword text. Please limit any outgoing links in your comment to a maximum of ONE, which should not be the same as you entered URL in the form. Please be considerate to other commenters. Please be relevant to the blog post and contribute to the discussion. Blatant link generation comments (we get a lot of those!) will be deleted. LICENSE By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution. Your comment may be edited or removed by a site admin if deemed necessary.
Nice article on how to customize Magento Layout in right way. It’s very useful for me. Thank you for sharing it.
Thanks for the info, also, you can clear the entire content section by using:
<reference name=”content”>
<action method=”unsetChildren” />
</reference>
Cheers!
The first solution which is creating another template is the most simplest there is. Recommended for people who has no idea of xml.
Nice article and very informative. Really helpful for our new Magento developers at Magento Technologies.
Really? You consider creating and maintaining an entire additional theme directory structure just for a single page variation as a simpler solution than editing a line or two?
@ndixon
Magento has hierarchical themes, so your new theme can consist of a single template file and everything else will refer to the higher up theme.
Nice article on how to customize Magento Layout in right way. It’s very useful for me. Thank you for sharing it.
Solution 3 is just what I’ve been looking for. Thanks!