PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Shipping/Model/Rate/Result.php

https://bitbucket.org/acidel/buykoala
PHP | 160 lines | 96 code | 15 blank | 49 comment | 7 complexity | a31b336461c7512624b0a470f5ad948e MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Shipping
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Shipping_Model_Rate_Result
  27. {
  28. protected $_rates = array();
  29. protected $_error = null;
  30. /**
  31. * Reset result
  32. */
  33. public function reset()
  34. {
  35. $this->_rates = array();
  36. return $this;
  37. }
  38. public function setError($error)
  39. {
  40. $this->_error = $error;
  41. }
  42. public function getError()
  43. {
  44. return $this->_error;
  45. }
  46. /**
  47. * Add a rate to the result
  48. *
  49. * @param Mage_Shipping_Model_Rate_Result_Abstract|Mage_Shipping_Model_Rate_Result $result
  50. */
  51. public function append($result)
  52. {
  53. if ($result instanceof Mage_Shipping_Model_Rate_Result_Error) {
  54. $this->setError(true);
  55. }
  56. if ($result instanceof Mage_Shipping_Model_Rate_Result_Abstract) {
  57. $this->_rates[] = $result;
  58. }
  59. elseif ($result instanceof Mage_Shipping_Model_Rate_Result) {
  60. $rates = $result->getAllRates();
  61. foreach ($rates as $rate) {
  62. $this->append($rate);
  63. }
  64. }
  65. return $this;
  66. }
  67. /**
  68. * Return all quotes in the result
  69. */
  70. public function getAllRates()
  71. {
  72. return $this->_rates;
  73. }
  74. /**
  75. * Return rate by id in array
  76. */
  77. public function getRateById($id)
  78. {
  79. return isset($this->_rates[$id]) ? $this->_rates[$id] : null;
  80. }
  81. /**
  82. * Return quotes for specified type
  83. *
  84. * @param string $type
  85. */
  86. public function getRatesByCarrier($carrier)
  87. {
  88. $result = array();
  89. foreach ($this->_rates as $rate) {
  90. if ($rate->getCarrier()===$carrier) {
  91. $result[] = $rate;
  92. }
  93. }
  94. return $result;
  95. }
  96. public function asArray()
  97. {
  98. $currencyFilter = Mage::app()->getStore()->getPriceFilter();
  99. $rates = array();
  100. $allRates = $this->getAllRates();
  101. foreach ($allRates as $rate) {
  102. $rates[$rate->getCarrier()]['title'] = $rate->getCarrierTitle();
  103. $rates[$rate->getCarrier()]['methods'][$rate->getMethod()] = array(
  104. 'title'=>$rate->getMethodTitle(),
  105. 'price'=>$rate->getPrice(),
  106. 'price_formatted'=>$currencyFilter->filter($rate->getPrice()),
  107. );
  108. }
  109. return $rates;
  110. }
  111. public function getCheapestRate()
  112. {
  113. $cheapest = null;
  114. $minPrice = 100000;
  115. foreach ($this->getAllRates() as $rate) {
  116. if (is_numeric($rate->getPrice()) && $rate->getPrice()<$minPrice) {
  117. $cheapest = $rate;
  118. $minPrice = $rate->getPrice();
  119. }
  120. }
  121. return $cheapest;
  122. }
  123. /**
  124. * Sort rates by price from min to max
  125. *
  126. * @return Mage_Shipping_Model_Rate_Result
  127. */
  128. public function sortRatesByPrice ()
  129. {
  130. if (!is_array($this->_rates) || !count($this->_rates)) {
  131. return $this;
  132. }
  133. /* @var $rate Mage_Shipping_Model_Rate_Result_Method */
  134. foreach ($this->_rates as $i => $rate) {
  135. $tmp[$i] = $rate->getPrice();
  136. }
  137. natsort($tmp);
  138. foreach ($tmp as $i => $price) {
  139. $result[] = $this->_rates[$i];
  140. }
  141. $this->reset();
  142. $this->_rates = $result;
  143. return $this;
  144. }
  145. }