Magento2如何修改默认的产品排序方式?

Magento2默认是老产品在产品分类的最前面,这样不符合中国人的使用习惯。
如果想更改为新上架的产品在分类的最前面,其实很简单,只需要修改模板的xml配置文件即可:
文件:

app/design/frontend/{{Vender_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">
<body>
    <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>
    <move element="category.view.container" destination="content" before="-"/>
</body>
</page>

如果这样不能满足你的需求,你也可以修改核心文件:
https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Catalog/Helper/Product/ProductList.php#L27

如果需要更多的排序方式,你可以看看Magento2的这个扩展:

由于Magento 2.4开始强制使用elasticsearch,以上办法已不适用。
请适用 土豆 分享的办法: