13

Ok so I have my categories sorting by price. Magento 2 defaults this to price ascending. But I would prefer it to be by price descending.

Pretty sure I can do this by customising catalog_category_view.xml in my theme and putting an argument to product_list_toolbar but I just can't quite figure it out. Any help?

1

3 Answers 3

27

Copy the file:

vendor/magento/module-catalog/view/frontend/layout/catalog_category_view.xml

to

app/design/frontend/{{Vender_Namespace}}/{{Theme_Name}}/Magento_Catalog/layout/catalog_category_view.xml

into your theme and add this to the file:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <referenceBlock name="category.products.list">
                <action method="setDefaultDirection">
                    <argument name="dir" xsi:type="string">desc</argument>
                </action>
            </referenceBlock>
        </referenceContainer>
        <move element="category.view.container" destination="content" before="-"/>
    </body>
</page>

The links that Manashvi point to the right locations in the core for the files that control this functionality, but they don't provide a solution. I didn't find this anywhere in the core code so i just took a guess based on the code in this file:

vendor/magento/module-catalog/Block/Product/ProductList/Toolbar.php

and used the syntax in other configuration in other .xml files.

I did try calling $block->setDefaultDirection('desc') in the file:

vendor/magento/module-catalog/view/frontend/templates/product/list/toolbar/sorter.phtml

file but this didn't work. I haven't tracked down why, but using the xml to set the default worked, so i just moved on.

6
  • 1
    Just a quick question @circlesix your solution works great, but I'd like to apply it to only a couple of categories. I thought I'd be able to drop in the referenceContainer section in to Custom Layout Update section for each category within the admin panel but this has no affect. Any ideas? Commented Jun 27, 2016 at 14:04
  • Haven't figured out a solution yet, i'll post here when i can come up with a better way to do this.
    – circlesix
    Commented Jun 28, 2016 at 22:50
  • 1
    @user1837290, you can just put the chunk starting and ending with referenceContainer in the "Layout Update XML" field in the admin of your specific categories. As long as you aren't overriding the sorting globally in your theme it should work. Related: magento.stackexchange.com/q/167048/2415 Commented Mar 31, 2017 at 15:40
  • 1
    @circlesix you only need to provide name attribute when referencing a block Commented Apr 12, 2018 at 6:42
  • 1
    Just wanted to comment and say this still works as of Magento 2.3, it just solved the issue for me! Commented Sep 19, 2019 at 21:20
7

You can use the shorter version as well. For specific categories you can use seperate files: catalog_category_view_id_X.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="category.products.list">
        <action method="setDefaultDirection">
            <argument name="dir" xsi:type="string">desc</argument>
        </action>
    </referenceBlock>
</body>
</page>
2
  • 's solution (using only the reference block and containing content) can also be added to Custom XML in the category if you don't want to make a custom layout file. Commented Mar 4, 2018 at 1:24
  • I definitely will proclaim this as the correct answer, HOWEVER, in some instances, product_list_toolbar (instead of category.products.list) will work, as I have just found out (after spending hours figuring it out) - presumably this can be due to upgrading from an older magento2 version (ie 2.3.2) meaning the incorrect reference block will be present. Commented Apr 26, 2022 at 12:31
1

You have to referenceBlock product_list_toolbar as the category.products.list does not have setDefaultDirection method.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
         <referenceBlock name="product_list_toolbar">
            <action method="setDefaultDirection">
                <argument name="dir" xsi:type="string">asc</argument>
            </action>
        </referenceBlock>
    </body>
</page>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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