Bước 1 : Đầu tiên bạn nên tạo đăng ký.php
Tên nhà cung cấp: Arun
Tên mô-đun: NewSorting
Nhà cung cấp / Modulename / register.php
<?php \Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Arun_NewSorting',
__DIR__
);?>
Bước 2 : Bạn tạo module.xml
Nhà cung cấp / Modulename / 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="Arun_NewSorting" setup_version="0.0.1">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>
Bước 3 : Bạn tạo plugin
Nhà cung cấp / Modulename / etc / 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="Arun_NewSorting::addCustomOptions" type="Arun\NewSorting\Plugin\Model\Config" />
</type>
<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
<plugin name="Arun_NewSorting::addPriceDecendingFilterInToolbar" type="Arun\NewSorting\Plugin\Product\ProductList\Toolbar" />
</type>
</config>
Bước 4 : sau đó tạo config.php
Nhà cung cấp / Modulename / Plugin / Model / Config.php
<?php
namespace Arun\NewSorting\Plugin\Model;
use Magento\Store\Model\StoreManagerInterface;
class Config {
protected $_storeManager;
public function __construct(
StoreManagerInterface $storeManager
) {
$this->_storeManager = $storeManager;
}
public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options)
{
$store = $this->_storeManager->getStore();
$currencySymbol = $store->getCurrentCurrency()->getCurrencySymbol();
// Remove specific default sorting options
$default_options = [];
$default_options['name'] = $options['name'];
unset($options['position']);
unset($options['name']);
unset($options['price']);
//Changing label
$customOption['position'] = __( 'Relevance' );
//New sorting options
$customOption['created_at'] = __( ' New' );
$customOption['name'] = $default_options['name'];
//Merge default sorting options with custom options
$options = array_merge($customOption, $options);
return $options;
}
}
Bước 5 : Ghi đè Thanh công cụ.php ***
Nhà cung cấp / Modulename / Plugin / Product / ProductList / Thanh công cụ.php
<?php
namespace Arun\NewSorting\Plugin\Product\ProductList;
class Toolbar
{
public function aroundSetCollection(
\Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
\Closure $proceed,
$collection
) {
$currentOrder = $subject->getCurrentOrder();
$result = $proceed($collection);
if ($currentOrder) {
if ($currentOrder == 'created_at') {
$subject->getCollection()->setOrder('created_at', 'desc');
}
}
return $result;
}
}
nó hoạt động hoàn hảo
} elseif ( $this->getCurrentDirection() == 'asc' ) {
thành} else {
.