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_Shipping
00029 {
00030
00031
00032
00033
00034
00035 protected $_orig = null;
00036
00037
00038
00039
00040
00041
00042 protected $_result = null;
00043
00044
00045
00046
00047
00048
00049
00050 public function getResult()
00051 {
00052 if (empty($this->_result)) {
00053 $this->_result = Mage::getModel('shipping/rate_result');
00054 }
00055 return $this->_result;
00056 }
00057
00058
00059
00060
00061 public function setOrigData($data)
00062 {
00063 $this->_orig = $data;
00064 }
00065
00066
00067
00068
00069 public function resetResult()
00070 {
00071 $this->getResult()->reset();
00072 return $this;
00073 }
00074
00075
00076
00077
00078
00079
00080 public function getConfig()
00081 {
00082 return Mage::getSingleton('shipping/config');
00083 }
00084
00085
00086
00087
00088
00089
00090
00091
00092 public function collectRates(Mage_Shipping_Model_Rate_Request $request)
00093 {
00094 if (!$request->getOrig()) {
00095 $request
00096 ->setCountryId(Mage::getStoreConfig('shipping/origin/country_id', $request->getStore()))
00097 ->setRegionId(Mage::getStoreConfig('shipping/origin/region_id', $request->getStore()))
00098 ->setCity(Mage::getStoreConfig('shipping/origin/city', $request->getStore()))
00099 ->setPostcode(Mage::getStoreConfig('shipping/origin/postcode', $request->getStore()));
00100 }
00101
00102 $limitCarrier = $request->getLimitCarrier();
00103 if (!$limitCarrier) {
00104 $carriers = Mage::getStoreConfig('carriers', $request->getStoreId());
00105
00106 foreach ($carriers as $carrierCode=>$carrierConfig) {
00107 $this->collectCarrierRates($carrierCode, $request);
00108 }
00109 } else {
00110 if (!is_array($limitCarrier)) {
00111 $limitCarrier = array($limitCarrier);
00112 }
00113 foreach ($limitCarrier as $carrierCode) {
00114 $carrierConfig = Mage::getStoreConfig('carriers/'.$carrierCode, $request->getStoreId());
00115 if (!$carrierConfig) {
00116 continue;
00117 }
00118 $this->collectCarrierRates($carrierCode, $request);
00119 }
00120 }
00121
00122 return $this;
00123 }
00124
00125 public function collectCarrierRates($carrierCode, $request)
00126 {
00127 $carrier = $this->getCarrierByCode($carrierCode, $request->getStoreId());
00128 if (!$carrier) {
00129 return $this;
00130 }
00131 $result = $carrier->checkAvailableShipCountries($request);
00132 if (false !== $result && !($result instanceof Mage_Shipping_Model_Rate_Result_Error)) {
00133 $result = $carrier->proccessAdditionalValidation($request);
00134 }
00135
00136
00137
00138
00139 if (false !== $result){
00140 if (!$result instanceof Mage_Shipping_Model_Rate_Result_Error) {
00141 $result = $carrier->collectRates($request);
00142 }
00143
00144 if (method_exists($result, 'sortRatesByPrice')) {
00145 $result->sortRatesByPrice();
00146 }
00147 $this->getResult()->append($result);
00148 }
00149 return $this;
00150 }
00151
00152 public function collectRatesByAddress(Varien_Object $address, $limitCarrier=null)
00153 {
00154 $request = Mage::getModel('shipping/rate_request');
00155 $request->setDestCountryId($address->getCountryId());
00156 $request->setDestRegionId($address->getRegionId());
00157 $request->setDestPostcode($address->getPostcode());
00158 $request->setPackageValue($address->getBaseSubtotal());
00159 $request->setPackageWeight($address->getWeight());
00160 $request->setFreeMethodWeight($address->getFreeMethodWeight());
00161 $request->setPackageQty($address->getItemQty());
00162 $request->setStoreId(Mage::app()->getStore()->getId());
00163 $request->setWebsiteId(Mage::app()->getStore()->getWebsiteId());
00164 $request->setBaseCurrency(Mage::app()->getStore()->getBaseCurrency());
00165 $request->setPackageCurrency(Mage::app()->getStore()->getCurrentCurrency());
00166
00167 $request->setLimitCarrier($limitCarrier);
00168
00169 return $this->collectRates($request);
00170 }
00171
00172 public function getCarrierByCode($carrierCode, $storeId = null)
00173 {
00174 if (!Mage::getStoreConfigFlag('carriers/'.$carrierCode.'/active', $storeId)) {
00175 return false;
00176 }
00177 $className = Mage::getStoreConfig('carriers/'.$carrierCode.'/model', $storeId);
00178 if (!$className) {
00179 return false;
00180 #Mage::throwException('Invalid carrier: '.$carrierCode);
00181 }
00182 $obj = Mage::getModel($className);
00183 if ($storeId) {
00184 $obj->setStore($storeId);
00185 }
00186 return $obj;
00187 }
00188
00189 }