在Magento 2默认功能中,类别页面的工具栏上将显示以下三种排序选项,
- Sort by “Position”
- Sort by “Product Name”
- Sort by “Price”
但在某些情况下,您可能希望在类别工具栏中添加更多新选项,例如按创建日期(最新),畅销产品等对产品进行分类。在类别工具栏中添加新的排序选项并不困难,这篇博客可以帮助您以更少的时间自定义代码。
在这篇博客中,将展示如何在类别页面的工具栏中添加新的排序选项“Newest”。为了在Magento 2中添加新的排序选项,我们必须使用以下两个类,Magento\Catalog\Model\Config和Magento\Catalog\Block\Product\ProductList\Toolbar
步骤1:在以下路径中创建名为registration.php的文件,
文件路径:app/code/Alwayly/NewSorting/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Alwayly_NewSorting',
__DIR__
);
步骤2:在以下路径中创建名为module.xml的文件,
文件路径:app/code/Alwayly/NewSorting/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Alwayly_MyAttributeSet" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>
步骤3:在以下文件路径中创建名为di.xml的文件,在此文件中,我们必须为以下原生类添加插件,Magento\Catalog\Model\Config&Magento\Catalog\Block\Product\ProductList\Toolbar
文件路径:app/code/Alwayly/NewSorting/etc/frontend/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Model\Config">
<plugin name="Alwayly_NewSorting::addNewSortOption" type="Alwayly\NewSorting\Plugin\Model\Config" />
</type>
<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
<plugin name="Alwayly_NewSorting::addNewFilterInToolbar" type="Alwayly\NewSorting\Plugin\Product\ProductList\Toolbar" />
</type>
</config>
步骤4:在以下文件路径中创建名为Config.php的文件,在此文件中,我们必须通过插件方法自定义原生代码以添加新的排序选项。
文件路径:app/code/Alwayly/NewSorting/Plugin/Model/Config.php
<?php
namespace Alwayly\NewSorting\Plugin\Model;
class Config
{
const NEWEST_SORT_BY = 'newest';
/**
* Adding new sort option
*
* @param \Magento\Catalog\Model\Config $catalogConfig
* @param [] $result
* @return []
*/
public function afterGetAttributeUsedForSortByArray(
\Magento\Catalog\Model\Config $subject,
$result
) {
return array_merge(
$result,
[
self::NEWEST_SORT_BY => __('Newest')
]
);
}
}
步骤 5: 最后,在以下文件路径中创建一个名为Toolbar.php的文件,在此文件中我们必须通过插件方法自定义原生代码以添加我们的新排序选项。.
文件路径: app/code/Alwayly/NewSorting/Plugin/Product/ProductList/Toolbar.php
<?php
namespace Alwayly\NewSorting\Plugin\Product\ProductList;
class Toolbar
{
const NEWEST_SORT_BY = 'newest';
/**
* Around Plugin
*
* @param \Magento\Catalog\Block\Product\ProductList\Toolbar $subject
* @param \Closure $proceed
* @param \Magento\Framework\Data\Collection $collection
* @return \Magento\Catalog\Block\Product\ProductList\Toolbar
*/
public function aroundSetCollection(
\Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
\Closure $proceed,
$collection
) {
$currentOrder = $subject->getCurrentOrder();
$currentDirection = $subject->getCurrentDirection();
$result = $proceed($collection);
if ($currentOrder == self::NEWEST_SORT_BY) {
if ($currentDirection == 'desc') {
$subject->getCollection()->setOrder('created_at', 'desc');
}
else {
$subject->getCollection()->setOrder('created_at', 'asc');
}
}
return $result;
}
}
完成上述步骤后,在Magento 2安装的根目录中运行以下SSH命令,
php bin/magento setup:upgrade
然后,清除所有Magento缓存并检查网站的产品列表页面。
Note:我们已在Magento 2.3上测试了上述代码.

