Magento 2 jak wyświetlić metody wysyłki sortować według ceny?
Dec 04 2020
Cześć, chcielibyśmy posortować i wyświetlić metody wysyłki według ceny od najniższej do najwyższej na stronie kasy.
Na przykład:
$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
Odnosząc się do tej koncepcji wtyczki, próbuję zmienić moje wyjście za pomocą wtyczki. Magento 2: Ukryj inne metody wysyłki, gdy dostępna jest bezpłatna wysyłka
<?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;
}
}
}

Odpowiedzi
2 NagarajuK Dec 10 2020 at 12:00
Poniższa logika działa dla mnie.
<?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
Nie jestem pewien, jak nazywa się atrybut kosztu dostawy (może być konieczne debugowanie), ale zakładając, że jest to „cena”, powinno to działać. Dodaj to powyżej zwrotu:
usort($all, function($a,$b){ return $a->price > $b->price; }); return $all;