Advanced layout updates for categories and products in Magento

Share

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:

1
2
3
<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:

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

1
2
3
4
5
<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():

1
2
3
4
$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:

1
2
3
4
5
<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():

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

Leave a Reply

You must be logged in to post a comment.