Result.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Shipping\Model\Rate;
  7. /**
  8. * Class Result
  9. * Container for Rates
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Result
  15. {
  16. /**
  17. * Shipping method rates
  18. *
  19. * @var array
  20. */
  21. protected $_rates = [];
  22. /**
  23. * Shipping errors
  24. *
  25. * @var null|bool
  26. */
  27. protected $_error = null;
  28. /**
  29. * @var \Magento\Store\Model\StoreManagerInterface
  30. */
  31. protected $_storeManager;
  32. /**
  33. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  34. */
  35. public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
  36. {
  37. $this->_storeManager = $storeManager;
  38. }
  39. /**
  40. * Reset result
  41. *
  42. * @return $this
  43. */
  44. public function reset()
  45. {
  46. $this->_rates = [];
  47. return $this;
  48. }
  49. /**
  50. * Set Error
  51. *
  52. * @param bool $error
  53. * @return void
  54. */
  55. public function setError($error)
  56. {
  57. $this->_error = $error;
  58. }
  59. /**
  60. * Get Error
  61. *
  62. * @return null|bool
  63. */
  64. public function getError()
  65. {
  66. return $this->_error;
  67. }
  68. /**
  69. * Add a rate to the result
  70. *
  71. * @param \Magento\Quote\Model\Quote\Address\RateResult\AbstractResult|\Magento\Shipping\Model\Rate\Result $result
  72. * @return $this
  73. */
  74. public function append($result)
  75. {
  76. if ($result instanceof \Magento\Quote\Model\Quote\Address\RateResult\Error) {
  77. $this->setError(true);
  78. }
  79. if ($result instanceof \Magento\Quote\Model\Quote\Address\RateResult\AbstractResult) {
  80. $this->_rates[] = $result;
  81. } elseif ($result instanceof \Magento\Shipping\Model\Rate\Result) {
  82. $rates = $result->getAllRates();
  83. foreach ($rates as $rate) {
  84. $this->append($rate);
  85. }
  86. }
  87. return $this;
  88. }
  89. /**
  90. * Return all quotes in the result
  91. *
  92. * @return \Magento\Quote\Model\Quote\Address\RateResult\Method[]
  93. */
  94. public function getAllRates()
  95. {
  96. return $this->_rates;
  97. }
  98. /**
  99. * Return rate by id in array
  100. *
  101. * @param int $id
  102. * @return \Magento\Quote\Model\Quote\Address\RateResult\Method|null
  103. */
  104. public function getRateById($id)
  105. {
  106. return isset($this->_rates[$id]) ? $this->_rates[$id] : null;
  107. }
  108. /**
  109. * Return quotes for specified type
  110. *
  111. * @param string $carrier
  112. * @return array
  113. */
  114. public function getRatesByCarrier($carrier)
  115. {
  116. $result = [];
  117. foreach ($this->_rates as $rate) {
  118. if ($rate->getCarrier() === $carrier) {
  119. $result[] = $rate;
  120. }
  121. }
  122. return $result;
  123. }
  124. /**
  125. * Converts object to array
  126. *
  127. * @return array
  128. */
  129. public function asArray()
  130. {
  131. if ($this->_storeManager->getStore()->getBaseCurrency()
  132. && $this->_storeManager->getStore()->getCurrentCurrency()
  133. ) {
  134. $currencyFilter = $this->_storeManager->getStore()->getCurrentCurrency()->getFilter();
  135. $currencyFilter->setRate(
  136. $this->_storeManager->getStore()->getBaseCurrency()->getRate(
  137. $this->_storeManager->getStore()->getCurrentCurrency()
  138. )
  139. );
  140. } elseif ($this->_storeManager->getStore()->getDefaultCurrency()) {
  141. $currencyFilter = $this->_storeManager->getStore()->getDefaultCurrency()->getFilter();
  142. } else {
  143. $currencyFilter = new \Magento\Framework\Filter\Sprintf('%s', 2);
  144. }
  145. $rates = [];
  146. $allRates = $this->getAllRates();
  147. foreach ($allRates as $rate) {
  148. $rates[$rate->getCarrier()]['title'] = $rate->getCarrierTitle();
  149. $rates[$rate->getCarrier()]['methods'][$rate->getMethod()] = [
  150. 'title' => $rate->getMethodTitle(),
  151. 'price' => $rate->getPrice(),
  152. 'price_formatted' => $currencyFilter->filter($rate->getPrice()),
  153. ];
  154. }
  155. return $rates;
  156. }
  157. /**
  158. * Get cheapest rate
  159. *
  160. * @return null|\Magento\Quote\Model\Quote\Address\RateResult\Method
  161. */
  162. public function getCheapestRate()
  163. {
  164. $cheapest = null;
  165. $minPrice = 100000;
  166. foreach ($this->getAllRates() as $rate) {
  167. if (is_numeric($rate->getPrice()) && $rate->getPrice() < $minPrice) {
  168. $cheapest = $rate;
  169. $minPrice = $rate->getPrice();
  170. }
  171. }
  172. return $cheapest;
  173. }
  174. /**
  175. * Sort rates by price from min to max
  176. *
  177. * @return $this
  178. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  179. */
  180. public function sortRatesByPrice()
  181. {
  182. if (!is_array($this->_rates) || !count($this->_rates)) {
  183. return $this;
  184. }
  185. /* @var $rate \Magento\Quote\Model\Quote\Address\RateResult\Method */
  186. foreach ($this->_rates as $i => $rate) {
  187. $tmp[$i] = $rate->getPrice();
  188. }
  189. natsort($tmp);
  190. foreach ($tmp as $i => $price) {
  191. $result[] = $this->_rates[$i];
  192. }
  193. $this->reset();
  194. $this->_rates = $result;
  195. return $this;
  196. }
  197. /**
  198. * Set price for each rate according to count of packages
  199. *
  200. * @param int $packageCount
  201. * @return $this
  202. */
  203. public function updateRatePrice($packageCount)
  204. {
  205. if ($packageCount > 1) {
  206. foreach ($this->_rates as $rate) {
  207. $rate->setPrice($rate->getPrice() * $packageCount);
  208. }
  209. }
  210. return $this;
  211. }
  212. }