Advanced layout updates for categories and products in Magento

Advanced layout updates for categories and products in Magento

Recently I had a task to modify sorting in only one category of Magento shop.
What I had to do is “only” to change which attribute will be used for default sorting and I needed to reverse ordering from ascending to descending.
Task seems simple, right?

All you need to do is add/edit your category and in admin panel set your new configuration:

Default Product Listing Sort By:

Custom Layout Update:

For custom layout update I needed to add this 3 lines:

<reference name="product_list_toolbar">
   <action method="setDefaultDirection"><dir>desc</dir></action>
</reference>

That works on Magento CE 1.4.2.0 and 1.5.0.1.
Issue was that client has Professional Edition 1.9.0.0 and that 3 lines just don’t work via custom layout update when you use it in admin panel, everything else works but:

   <action method="setDefaultDirection"><dir>desc</dir></action>

JUST DOES NOT WORK!!!

I have described how to update layout of your custom controller in post
so can update anything trough your “local.xml” file.

Problem is that then you update category controller “catalog_category_default”, you will update all categories:

<catalog_category_default translate="label">
<reference name="product_list_toolbar">
   <action method="setDefaultDirection"><dir>desc</dir></action>
</reference>
</catalog_category_default>

and I needed only one to update with this particularly action method, so I opened Category and Products controllers and started to tracing…

I wanted to add my own handle, but that was requiring rewriting existing CategoryController class in my new module which I needed to create and configure, and I said it seemed very simple task.

Then I found out in CategoryController viewAction():

            $this->addActionLayoutHandles();
            $update->addHandle($category->getLayoutUpdateHandle());
            $update->addHandle('CATEGORY_' . $category->getId());
            $this->loadLayoutUpdates();

what was pretty interesting to me and my problem was solved with:

	<CATEGORY_20>
		<reference name="product_list_toolbar">
					<action method="setDefaultDirection"><dir>desc</dir></action>
			</reference>
	</CATEGORY_20>

Keyword is CATEGORY_20, 20 is id of category.

Same thing you can do with single products, you can found it at ProductController viewAction():

$update->addHandle('PRODUCT_'.$product->getId());

Enjoy coding!

You made it all the way down here so you must have enjoyed this post! You may also like:

Solr and Magento – search by department Domagoj Potkoc
Domagoj Potkoc, | 1

Solr and Magento – search by department

Magento 2 Custom CMS page layout Matej Djakovic
Matej Djakovic, | 7

Magento 2 Custom CMS page layout

Override Magento 2 Layout: Product page example Mladen Ristic
, | 6

Override Magento 2 Layout: Product page example

17 comments

  1. This works great for me under 1.6.2:

    <reference name="product_list_toolbar">
       <action method="setDefaultDirection" before="product_list_toolbar_pager"><dir>desc</dir></action>
    </reference>
  2. Is it possible to add layout updates to individual pages within a category? For example to display 4 products on the first page, then 12 products per page for the rest of that category?

    I don’t know if viewAction() allows you to add in something like the follwing in catalog.xml:

    <CATEGORY_20_page=1>
        <reference name="something">
                    <action method="ssomething"><dir>desc</dir></action>
            </reference>
    </CATEGORY_20_page=1>
  3. @verdan…that will show me just 1 product but i want to have all the products of the category shown with its options.

  4. @raju

    copy/paste code from:

    catalog/product/view.phtml
    to
    catalog/product/list.phtml

    hint: enable template path hints to see exactly which files you need to modify

  5. Hi, any idea how to change the category display mode for selected categories using xml layout updates?

    Default setting is ‘grid’, but I need to change it to ‘table’ for selected categories.
    Any suggestions are welcome.

  6. <catalog_product_view translate="label">
             <label>Catalog Product View (Any)</label>
            <CATEGORY_55>
                    <reference name="content">
                        <block type="catalog/product_view" name="product.info" template="catalog/product/view-posters.phtml">
                        </block>
                    </reference>
            </CATEGORY_55>>
       </catalog_product_view>
  7. Hi, really dig your blog, great stuff. In regards to this post topic, I have a few categories that need be presented differently on the product details page, basically i need to use an altered view.phtml for some categories. How can i apply this in the custom layout update or catalog.xml template? Can I do something like this

    Catalog Product View (Any)

    >

  8. Hi,

    I use your Slide categories extension with EM Computer Store template. The template comes with magento 1.4.0.1 version. I am new in magento but I update my magento 1.5.0.1. And I reinstall the extension. It is working in frontpage. The menu catalog -> Featured Products also work but in settings panel the site says 404 the site not found error. Are there any solution for this. Thank you for this wonderful extension.

    Greetings from Turkey 🙂

  9. Yeah, have you guys looked into why those layout updates don’t work, I don’t get it and didn’t have a lot of time to figure it out, so I did the same thing as you.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.