Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Result.php
Go to the documentation of this file.
1 <?php
7 
15 class Result
16 {
22  protected $_rates = [];
23 
29  protected $_error = null;
30 
34  protected $_storeManager;
35 
39  public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
40  {
41  $this->_storeManager = $storeManager;
42  }
43 
49  public function reset()
50  {
51  $this->_rates = [];
52  return $this;
53  }
54 
61  public function setError($error)
62  {
63  $this->_error = $error;
64  }
65 
71  public function getError()
72  {
73  return $this->_error;
74  }
75 
82  public function append($result)
83  {
84  if ($result instanceof \Magento\Quote\Model\Quote\Address\RateResult\Error) {
85  $this->setError(true);
86  }
87  if ($result instanceof \Magento\Quote\Model\Quote\Address\RateResult\AbstractResult) {
88  $this->_rates[] = $result;
89  } elseif ($result instanceof \Magento\Shipping\Model\Rate\Result) {
90  $rates = $result->getAllRates();
91  foreach ($rates as $rate) {
92  $this->append($rate);
93  }
94  }
95  return $this;
96  }
97 
103  public function getAllRates()
104  {
105  return $this->_rates;
106  }
107 
114  public function getRateById($id)
115  {
116  return isset($this->_rates[$id]) ? $this->_rates[$id] : null;
117  }
118 
125  public function getRatesByCarrier($carrier)
126  {
127  $result = [];
128  foreach ($this->_rates as $rate) {
129  if ($rate->getCarrier() === $carrier) {
130  $result[] = $rate;
131  }
132  }
133  return $result;
134  }
135 
141  public function asArray()
142  {
143  if ($this->_storeManager->getStore()->getBaseCurrency()
144  && $this->_storeManager->getStore()->getCurrentCurrency()
145  ) {
146  $currencyFilter = $this->_storeManager->getStore()->getCurrentCurrency()->getFilter();
147  $currencyFilter->setRate(
148  $this->_storeManager->getStore()->getBaseCurrency()->getRate(
149  $this->_storeManager->getStore()->getCurrentCurrency()
150  )
151  );
152  } elseif ($this->_storeManager->getStore()->getDefaultCurrency()) {
153  $currencyFilter = $this->_storeManager->getStore()->getDefaultCurrency()->getFilter();
154  } else {
155  $currencyFilter = new \Magento\Framework\Filter\Sprintf('%s', 2);
156  }
157  $rates = [];
158  $allRates = $this->getAllRates();
159  foreach ($allRates as $rate) {
160  $rates[$rate->getCarrier()]['title'] = $rate->getCarrierTitle();
161  $rates[$rate->getCarrier()]['methods'][$rate->getMethod()] = [
162  'title' => $rate->getMethodTitle(),
163  'price' => $rate->getPrice(),
164  'price_formatted' => $currencyFilter->filter($rate->getPrice()),
165  ];
166  }
167  return $rates;
168  }
169 
175  public function getCheapestRate()
176  {
177  $cheapest = null;
178  $minPrice = 100000;
179  foreach ($this->getAllRates() as $rate) {
180  if (is_numeric($rate->getPrice()) && $rate->getPrice() < $minPrice) {
181  $cheapest = $rate;
182  $minPrice = $rate->getPrice();
183  }
184  }
185  return $cheapest;
186  }
187 
194  public function sortRatesByPrice()
195  {
196  if (!is_array($this->_rates) || !count($this->_rates)) {
197  return $this;
198  }
199  /* @var $rate \Magento\Quote\Model\Quote\Address\RateResult\Method */
200  foreach ($this->_rates as $i => $rate) {
201  $tmp[$i] = $rate->getPrice();
202  }
203 
204  natsort($tmp);
205 
206  foreach ($tmp as $i => $price) {
207  $result[] = $this->_rates[$i];
208  }
209 
210  $this->reset();
211  $this->_rates = $result;
212  return $this;
213  }
214 
221  public function updateRatePrice($packageCount)
222  {
223  if ($packageCount > 1) {
224  foreach ($this->_rates as $rate) {
225  $rate->setPrice($rate->getPrice() * $packageCount);
226  }
227  }
228 
229  return $this;
230  }
231 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14
$storeManager
$price
$rates
Definition: tax.phtml:35
__construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
Definition: Result.php:39
$i
Definition: gallery.phtml:31