如何修改Magento2的默认产品排序为新产品在前面?

如何修改Magento2的默认产品排序为新的在前面?

我之前在这里已经说过怎么修改Magento2的默认排序,
但是博客的代码解析被转义了,我这里再说一次:

首先登录服务器,切换到您Magento2的根目录.

1.首先备份需要修改的文件

cp vendor/magento/module-catalog/Block/Product/ProductList/Toolbar.php vendor/magento/module-catalog/Block/Product/ProductList/Toolbar.php.backup

2.编辑Toolbar.php文件.

vi vendor/magento/module-catalog/Block/Product/ProductList/Toolbar.php

由于版本不同,我这里大概在192行左右,找到方法setCollection下的:

if ($this->getCurrentOrder()) {

替换为:

        if ($this->getCurrentOrder()) {
    if(($this->getCurrentOrder())=='position'){
            $this->_collection->setOrder('entity_id','desc');
    }
    else {
            $this->_collection->setOrder($this->getCurrentOrder(),$this->getCurrentDirection());
    }
}

保存退出.

3.清楚缓存和重新索引

运行:bin/magento ca:fl && bin/magento index:re

PS.由于本方式是直接修改core文件,建议大概copy到local文件再修改。

1 个赞

这个文章已经过期,建议阅读下面的内容: