Magento 2 What is the different between setDefaultOrder and SetSortBy in layout xml?
-
07-03-2021 - |
Question
I'm trying to set a default sort order to products listing in category page.
I have seen people suggest using
<action method="setDefaultOrder">
<argument name="sort" xsi:type="string">name</argument>
</action>
<action method="setDefaultDirection">
<argument name="dir" xsi:type="string">asc</argument>
</action>
or by using
<action method="setSortBy"><string>name</string></action>
<action method="setDefaultDirection">
<argument name="dir" xsi:type="string">asc</argument>
</action>
Unfortunately none of the above method worked for me. But I just want to know what's the different between setDefaultOrder and setSortBy?
Solution
Not sure where you've got one of those snippets from
https://www.customerparadigm.com/changing-default-product-sorting-in-magento-2/
app/design/frontend/{{Vendor_Namespace}}/{{Theme_Name}}/Magento_Catalog/layout/catalog_category_view.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">
<referencecontainer name="content">
<referenceblock class="Magento\Catalog\Block\Product\ListProduct" name="category.products.list" as="product_list">
<action method="setDefaultDirection">
<argument name="dir" xsi:type="string">desc</argument>
</action>
</referenceblock>
</referencecontainer>
</page>
That's to change sort by direction
To change sort by value
Stores > Config > Catalog > Catalog > Storefront > Product Listing Sort By
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange