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;