Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CheapestMethodDeferredChooser.php
Go to the documentation of this file.
1 <?php
7 
10 
15 {
16  const METHOD_CODE = 'cheapest';
17 
21  public function choose(Address $address)
22  {
23  $address->setCollectShippingRates(true);
24  $address->collectShippingRates();
25  $shippingRates = $address->getAllShippingRates();
26 
27  if (empty($shippingRates)) {
28  return null;
29  }
30 
31  $cheapestRate = $this->selectCheapestRate($shippingRates);
32  return $cheapestRate->getCode();
33  }
34 
41  private function selectCheapestRate(array $shippingRates) : Rate
42  {
43  $rate = array_shift($shippingRates);
44  foreach ($shippingRates as $tmpRate) {
45  if ($tmpRate->getPrice() < $rate->getPrice()) {
46  $rate = $tmpRate;
47  }
48  }
49  return $rate;
50  }
51 }
$address
Definition: customer.php:38