I am trying to disable the default sorting based on price, that is happening on shipping methods.
I found that this is happening in sortRatesByPrice()
vendor/magento/module-shipping/Model/Rate/Result.php
public function sortRatesByPrice()
{
if (!is_array($this->_rates) || !count($this->_rates)) {
return $this;
}
/* @var $rate \Magento\Quote\Model\Quote\Address\RateResult\Method */
foreach ($this->_rates as $i => $rate) {
$tmp[$i] = $rate->getPrice();
}
natsort($tmp);
foreach ($tmp as $i => $price) {
$result[] = $this->_rates[$i];
}
$this->reset();
$this->_rates = $result;
return $this;
}
I basically just want to disable the sorting that happens here. (I.e. comment natsort($tmp)
)
I have tried to create a plugin for this, but this is not working.
This is my 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\Shipping\Model\Rate\Result">
<plugin name="dm_shipping_sortshipping" type="DM\Shipping\Plugin\Rate\Result" sortOrder="1" disabled="false" />
</type>
</config>
This is my plugin
<?php
namespace DM\Shipping\Plugin\Rate;
class Result {
public function beforeSortRatesByPrice()
{
$writer = new \Laminas\Log\Writer\Stream(BP . '/var/log/dm_debug.log');
$logger = new \Laminas\Log\Logger();
$logger->addWriter($writer);
$logger->info('beforeSortRatesByPrice');
}
public function aroundSortRatesByPrice()
{
$writer = new \Laminas\Log\Writer\Stream(BP . '/var/log/dm_debug.log');
$logger = new \Laminas\Log\Logger();
$logger->addWriter($writer);
$logger->info('aroundSortRatesByPrice');
}
public function afterSortRatesByPrice()
{
$writer = new \Laminas\Log\Writer\Stream(BP . '/var/log/dm_debug.log');
$logger = new \Laminas\Log\Logger();
$logger->addWriter($writer);
$logger->info('beforeSortRatesByPrice');
}
}
Why are none of these firing? Log is empty.