Magento 2配送方法を表示するには、価格で注文を並べ替えますか?

Dec 04 2020

こんにちは。チェックアウトページで、送料を低価格から高価格の順に並べ替えて表示したいと思います。

例えば:

$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

プラグインを使用して出力を変更しようとしているこのプラグインの概念を参照します。Magento 2:送料無料が利用可能な場合は他の配送方法を非表示にする

<?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;
        } 
    }
}

回答

2 NagarajuK Dec 10 2020 at 12:00

以下のロジックは私のために働きます。

    <?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;
          } 
        
    }
bjornredemption Dec 09 2020 at 15:46

送料属性が何と呼ばれるかはわかりませんが(デバッグが必要な場合があります)、「価格」であると仮定すると、これは機能するはずです。リターンの上にこれを追加します:

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