0

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.

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').

| improve this answer | |
0

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

| improve this answer | |
  • I know about that but I have to change only by Layout for custom products list. – m2dev Mar 19 at 7:06
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>
| improve this answer | |

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.