00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Mage_Shipping_Model_Rate_Result
00029 {
00030 protected $_rates = array();
00031 protected $_error = null;
00032
00033
00034
00035
00036 public function reset()
00037 {
00038 $this->_rates = array();
00039 return $this;
00040 }
00041
00042 public function setError($error)
00043 {
00044 $this->_error = $error;
00045 }
00046
00047 public function getError()
00048 {
00049 return $this->_error;
00050 }
00051
00052
00053
00054
00055
00056
00057 public function append($result)
00058 {
00059 if ($result instanceof Mage_Shipping_Model_Rate_Result_Abstract) {
00060 $this->_rates[] = $result;
00061 }
00062 elseif ($result instanceof Mage_Shipping_Model_Rate_Result) {
00063 $rates = $result->getAllRates();
00064 foreach ($rates as $rate) {
00065 $this->append($rate);
00066 }
00067 }
00068 return $this;
00069 }
00070
00071
00072
00073
00074 public function getAllRates()
00075 {
00076 return $this->_rates;
00077 }
00078
00079
00080
00081
00082 public function getRateById($id)
00083 {
00084 return isset($this->_rates[$id]) ? $this->_rates[$id] : null;
00085 }
00086
00087
00088
00089
00090
00091
00092 public function getRatesByCarrier($carrier)
00093 {
00094 $result = array();
00095 foreach ($this->_rates as $rate) {
00096 if ($rate->getCarrier()===$carrier) {
00097 $result[] = $rate;
00098 }
00099 }
00100 return $result;
00101 }
00102
00103 public function asArray()
00104 {
00105 $currencyFilter = Mage::app()->getStore()->getPriceFilter();
00106 $rates = array();
00107 $allRates = $this->getAllRates();
00108 foreach ($allRates as $rate) {
00109 $rates[$rate->getCarrier()]['title'] = $rate->getCarrierTitle();
00110 $rates[$rate->getCarrier()]['methods'][$rate->getMethod()] = array(
00111 'title'=>$rate->getMethodTitle(),
00112 'price'=>$rate->getPrice(),
00113 'price_formatted'=>$currencyFilter->filter($rate->getPrice()),
00114 );
00115 }
00116 return $rates;
00117 }
00118
00119 public function getCheapestRate()
00120 {
00121 $cheapest = null;
00122 $minPrice = 100000;
00123 foreach ($this->getAllRates() as $rate) {
00124 if (is_numeric($rate->getPrice()) && $rate->getPrice()<$minPrice) {
00125 $cheapest = $rate;
00126 $minPrice = $rate->getPrice();
00127 }
00128 }
00129 return $cheapest;
00130 }
00131
00132
00133
00134
00135
00136
00137 public function sortRatesByPrice ()
00138 {
00139 if (!is_array($this->_rates) || !count($this->_rates)) {
00140 return $this;
00141 }
00142
00143 foreach ($this->_rates as $i => $rate) {
00144 $tmp[$i] = $rate->getPrice();
00145 }
00146
00147 natsort($tmp);
00148
00149 foreach ($tmp as $i => $price) {
00150 $result[] = $this->_rates[$i];
00151 }
00152
00153 $this->reset();
00154 $this->_rates = $result;
00155 return $this;
00156 }
00157 }