10

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?

22

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.

  • 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? – caffeinehigh Jun 27 '16 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 Jun 28 '16 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 – Scruffy Paws Mar 31 '17 at 15:40
  • 1
    @circlesix you only need to provide name attribute when referencing a block – Miguel Felipe Guillen Calo Apr 12 '18 at 6:42
  • Right!!! I just found another post here that ran into issues with my answer. I have since updated my answer with the correct xml. Thanks! – circlesix Apr 27 '18 at 15:35
4

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>
  • '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. – Phil Birnie Mar 4 '18 at 1:24

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.