4

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?


Update #1

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 catalog_category_view.xml file...

app/code/design/frontend/<vendor_name>/<theme_name>/Magento_Catalog/layout/catalog_category_view.xml

<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 asc to desc, of course). Is this expected behavior? Does the extended theme file trump the Layout Update XML settings from the admin?


Update #2

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?

3

The code is working.

I tried the code and it works fine on my magento 2.1.3 CE.

Make sure your Design tab looks like this:

enter image description here

What you can try:

  1. Revert to developer mode.
  2. Clear magento cache.
  3. Clear browser cache.
  • Thanks for your reply! You are right. I tested on a clean install and it worked great. I dug further and tracked it down to an extended layout file we have in our theme for catalog_category_view.xml. I updated my question with further details since it still seems like there might be an issue... unless what I describe in the update is expected behavior. Thoughts? – Scruffy Paws Mar 31 '17 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.