I need to update sorting on just one category in our Magento CE 2.1.3 site. I tried placing the following code in the Layout Update XML field on the category in the admin, but it doesn't seem to do anything. If I put this same thing in an XML file in my theme it works like a charm, although it changes sorting throughout the whole site.
<referenceContainer name="content"> <referenceBlock name="product_list_toolbar"> <action method="setDefaultDirection"> <argument name="dir" xsi:type="string">asc</argument> </action> </referenceBlock> </referenceContainer>
As an alternative, I know in Magento 1 you can put some category specific layout updates by category ID using the
<CATEGORY_123>, so does Magento 2 have something similar?
After testing the code above on a clean install I confirmed that it does indeed work. I tracked it down to the fact that our theme is extending the
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="content"> <referenceBlock name="product_list_toolbar"> <action method="setDefaultDirection"> <argument name="dir" xsi:type="string">desc</argument> </action> </referenceBlock> </referenceContainer> </body> </page>
If I remove the
<action method="setDefaultDirection"> section, the "Layout Update XML" code I'm using in the admin seems to work great (only switching
desc, of course). Is this expected behavior? Does the extended theme file trump the Layout Update XML settings from the admin?
Regarding my question for an alternative to the Magento 1 category id specific handle
<CATEGORY_123>, thanks to this link I found that you can do this by using an ID specific file. Instead of the global
catalog_category_view.xml you can create a file called
catalog_category_view_id_123.xml to update that specific category ID.
My question still stands, though, as to why the admin "Layout Update XML" field is ignored if you have a theme specific sorting set in an XML file. It seems broken to me, but maybe it's expected behavior?