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:
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!
17 comments
Hi…
How can I do the same with M2?
No!
Magento 2 has different codebase.
This works great for me under 1.6.2:
This works great for me under 1.6.2:
desc
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:
@verdan…that will show me just 1 product but i want to have all the products of the category shown with its options.
@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
@ verdan- can u tell me how?
@raju sure there is
Is there any way to show the PRODUCT OPTIONS of configurable products in the category page?
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.
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)
>
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 🙂
Hi, I had the same problem when trying to set a custom background on all subcategories and products. In the end I put everything into a (free) module which extends Product- and Category-Model to inherit Layout Updates which are defined somewhere higher in the hierarchy:
http://www.exanto.de/magento-module-und-hacks.html
Greetings from Germany!
@Dan
you have the solution described above
for fixes you will need to go to this address:
http://www.magentocommerce.com/bug-tracking/report/
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.