2

Hi we would like to sort and display the shipping methods by price Low to High on checkout page.

For example:

$31.72  Express Saver Federal Express
$37.50  2 DayFederal Express
$80.47  Standard Overnight Federal Express
$87.77  Priority Overnight Federal Express
$5.00   Fixed Flat Rate
$5.00   Media Mail Select Shipping Method
$10.00  FedEx Home Delivery Select Shipping Method

Referring this plugin concept trying to change my output using plugin. Magento 2: Hide other shipping methods when free shipping is available

<?php
namespace Demo\Test\Plugin\Model;
use Magento\Framework\Session\Generic;


class ShippingMethodManagement {

    /**
     * @var Generic
    */
    private $coreSession;

    /**
     * @param \Magento\Framework\Session\Generic $sessionManager
    */
    public function __construct(
        Generic $sessionManager
    ) {
        $this->coreSession = $sessionManager;    
    }

    public function afterEstimateByExtendedAddress($shippingMethodManagement, $output)
    {
        return $this->filterOutput($output);
    }
    public function afterEstimateByAddress($shippingMethodManagement, $output)
    {
        return $this->filterOutput($output);
    }
    public function afterEstimateByAddressId($shippingMethodManagement, $output)
    {
        return $this->filterOutput($output);
    }
    private function filterOutput($output)
    {
       //Here i would like to sort shipping methods by shipping amount
     
        $all = [];
        
            foreach ($output as $shippingMethod) {               

                $all[] = $shippingMethod;
            } 

            return $all;
        } 
    }
}

enter image description here

3
  • What magento2 version you using?
    – KestutisK
    Dec 4 '20 at 17:27
  • Magento 2.3.5 ee Dec 5 '20 at 3:19
  • Have you tried to debug it? Because it should sort by price. vendor/magento/module-shipping/Model/Rate/CarrierResult.php here you find getAllRates. Try to add breakpoint here with xdebug and check what you get and what it does.
    – KestutisK
    Dec 7 '20 at 13:30
2

The below logic works for me.

    <?php
    namespace Demo\Test\Plugin\Model;
    use Magento\Framework\Session\Generic;
    
    
    class ShippingMethodManagement {
    
        /**
         * @var Generic
        */
        private $coreSession;
    
        /**
         * @param \Magento\Framework\Session\Generic $sessionManager
        */
        public function __construct(
            Generic $sessionManager
        ) {
            $this->coreSession = $sessionManager;    
        }
    
        public function afterEstimateByExtendedAddress($shippingMethodManagement, $output)
        {
            return $this->filterOutput($output);
        }
        public function afterEstimateByAddress($shippingMethodManagement, $output)
        {
            return $this->filterOutput($output);
        }
        public function afterEstimateByAddressId($shippingMethodManagement, $output)
        {
            return $this->filterOutput($output);
        }
        private function filterOutput($output)
        {
           //Here i would like to sort shipping methods by shipping amount
         
            $all = [];
            
                foreach ($output as $shippingMethod) {               
    
                    $all[$shippingMethod->getAmount()] = $shippingMethod;
                } 
                ksort($all);    
                return $all;
          } 
        
    }
1
0

Not sure what the shipping cost attribute would be called (you may need to debug) however assuming it is 'price' this should work. Add this above the return :

usort($all, function($a,$b){
     return $a->price > $b->price;
});
return $all;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.