Krok 1 : Najpierw powinieneś utworzyć register.php
Nazwa dostawcy: Arun
Nazwa modułu: NewSorting
Dostawca / nazwa modulatora / rejestracja.php
<?php \Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Arun_NewSorting',
__DIR__
);?>
Krok 2 : Utwórz plik module.xml
Vendor / 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>
Krok 3 : Tworzysz wtyczkę
Dostawca / nazwa modulatora / 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>
Krok 4 : Następnie utwórz config.php
Producent / nazwa modulatora / wtyczka / 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;
}
}
Krok 5 : Zastąp Toolbar.php ***
Producent / nazwa modulatora / wtyczka / produkt / lista produktów / Toolbar.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;
}
}
działa idealnie
} elseif ( $this->getCurrentDirection() == 'asc' ) {
na} else {
.