1

I have added the listing of products on the custom page. By default sort by is Position. I have to change it to By default by Name.

<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>

Above code is working for the direction but when I'm using setDefaultSortBy getting the error it's invalid.

3 Answers 3

1

You can change default sort by from Magento Admin

Magento Admin -> Stores -> Configuration -> Catalog -> Catalog -> Storefront -> Product Listing Sort by -> change it "Product Name" -> Save Config

enter image description here

1
  • I know about that but I have to change only by Layout for custom products list.
    – m2dev
    Mar 19, 2020 at 7:06
0

You can do this by calling a product collection's addAttributeToSort method for each ordering.

Format: $ProductCollectionObject->addAttributeToSort('attribute_code', 'sort_order')

For example, wherever you see in a template, $_category->getProductCollection() or $_product->getCollection() you can then add either addAttributeToSort('name', 'asc') or addAttributeToSort('name', 'desc').

0
  1. Make your attribute(name) 'Used for Sorting in Product Listing' to Yes
  2. Just add this block in referenceBlock
            <action method="setSortBy">
                <argument name="attribute" xsi:type="string">name</argument>
            </action>

Your Answer

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

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