Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.95 KB | None | 0 0
  1. public function collectRates(Mage_Shipping_Model_Rate_Request $request)
  2. {
  3. if (!Mage::getStoreConfig('carriers/'.$this->_code.'/active')) {
  4. return false;
  5. }
  6.  
  7. $price = $this->getConfigData('price'); // set a default shipping price maybe 0
  8. $price = 0;
  9.  
  10. //var_dump($request);
  11. // $api_url = "http://www.eshipper.com/fetch_rates";
  12. // $api_username = "opsapi";
  13. // $api_password = "opsapipass";
  14. $api_url = $this->getConfigData('api');
  15. $api_username = $this->getConfigData('apiusername');
  16. $api_password = $this->getConfigData('apipassword');
  17. $increase_rate = $this->getConfigData('increaserate');
  18. $weight_class = $this->getConfigData('weightclass');
  19.  
  20. //Case1: Price Depends on Country,State and Pin Code
  21. $destCountry = $request->getDestCountryId();
  22. $destRegion = $request->getDestRegionId();
  23. $destRegionCode = $request->getDestRegionCode();
  24. // print_r($destStreet = $request->getDestStreet()); echo ': Dest Street<br/>';
  25. $destStreet = $request->getDestStreet();
  26. $destCity = $request->getDestCity();
  27. $destPostcode = $request->getDestPostcode();
  28. $country_id = $request->getCountryId();
  29. $region_id = $request->getRegionId();
  30. //$region_code = $request->getRegionCode();
  31. $city = $request->getCity();
  32. $postcode = $request->getPostcode();
  33.  
  34. $region = Mage::getModel('directory/region')->load($region_id);
  35. $region_code = $region->getCode();
  36.  
  37. $service_url = 'http://maps.googleapis.com/maps/api/geocode/json?address='.$destPostcode.'&sensor=false';
  38. $curl = curl_init($service_url);
  39. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  40. $curl_response = curl_exec($curl);
  41. if ($curl_response === false) {
  42. $info = curl_getinfo($curl);
  43. curl_close($curl);
  44. die('error occured during curl exec. Additioanl info: ' . var_export($info));
  45. }
  46. curl_close($curl);
  47. $decoded = json_decode($curl_response);
  48. if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
  49. die('error occured: ' . $decoded->response->errormessage);
  50. }
  51.  
  52. $result = $decoded->results;
  53. $city_name = $result[0]->address_components[1]->long_name;
  54.  
  55. if ($request->getAllItems()) {
  56.  
  57. $total_item = count($request->getAllItems());
  58. $currency = Mage::app()->getStore()->getCurrentCurrencyCode();
  59. $prod_html = '';
  60. $count_row = 1;
  61. foreach ($request->getAllItems() as $item) {
  62.  
  63. if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
  64. continue;
  65. }
  66.  
  67. if ($item->getHasChildren() && $item->isShipSeparately()) {
  68. $count_children = count($item->getHasChildren());
  69. $count_children_row = 1;
  70. foreach ($item->getChildren() as $child) {
  71. if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) {
  72. $product_id = $child->getProductId();
  73. $productObj = Mage::getModel('catalog/product')->load($product_id);
  74. $ship_price = $productObj->getData('shipping_price'); //our shipping attribute code
  75. //$price += (float)$ship_price;
  76. $prd_name = str_replace('"',"'",$productObj->getName());
  77. $prod_html .= '{"name":"'.$prd_name.'","sku":"'.$productObj->getSku().'","quantity":'.$item->getQty().',"weight":'.floor($item->getWeight()).',"weightUnit":"'.$weight_class.'","price":'.$item->getPrice().',"vendor":"null","requires_shipping":true,"taxable":true,"fulfillment_service":"manual","product_id":"'.$product_id.'","variant_id":410333945}';
  78. if($count_children_row<$count_children || $count_row<$total_item){
  79. $prod_html .= ',';
  80. }
  81. $count_children_row++;
  82. }
  83. }
  84. } else {
  85. $product_id = $item->getProductId();
  86. $productObj = Mage::getModel('catalog/product')->load($product_id);
  87. $prd_name = str_replace('"',"'",$productObj->getName());
  88. $prod_html .= '{"name":"'.$prd_name.'","sku":"'.$productObj->getSku().'","quantity":'.$item->getQty().',"weight":'.floor($item->getWeight()).',"weightUnit":"'.$weight_class.'","price":'.$item->getPrice().',"vendor":"null","requires_shipping":true,"taxable":true,"fulfillment_service":"manual","product_id":"'.$product_id.'","variant_id":410333945}';
  89. }
  90. if($count_row<$total_item){
  91. $prod_html .= ',';
  92. }
  93. $count_row++;
  94. }
  95. }
  96.  
  97.  
  98. //$fileContents = '{"rate":{"origin":{"country":"CA","postal_code":"V3N4R3","province":"ON","city":"Burnaby","name":null,"address1":"90 Maverick","address2":null,"address3":null,"phone":null,"fax":null,"address_type":null,"company_name":null},"destination":{"country":"'.$destCountry.'","postal_code":"'.$destPostcode.'","province":"'.$destRegionCode.'","city":"'.$destCity.'","name":null,"address1":null,"address2":"","address3":null,"phone":null,"fax":null,"address_type":null,"company_name":""},"items":['.$prod_html.'],"currency":"'.$currency.'"}}';
  99. $fileContents = '{"rate":{"origin":{"country":"'.$country_id.'","postal_code":"'.$postcode.'","province":"'.$region_code.'","city":"'.$city.'","name":null,"address1":null,"address2":null,"address3":null,"phone":null,"fax":null,"address_type":null,"company_name":null},"destination":{"country":"'.$destCountry.'","postal_code":"'.$destPostcode.'","province":"'.$destRegionCode.'","city":"'.$city_name.'","name":null,"address1":null,"address2":"","address3":null,"phone":null,"fax":null,"address_type":null,"company_name":""},"items":['.$prod_html.'],"currency":"'.$currency.'","CART_NAME":"MAGENTO"}}';
  100. //print_r($fileContents); exit;
  101.  
  102. $ch = curl_init($api_url);
  103. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  104. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  105. 'Content-Type: application/json',
  106. 'API_USERNAME: ' . $api_username,
  107. 'API_PASSWORD: ' . $api_password
  108. )
  109. );
  110. curl_setopt($ch, CURLOPT_POSTFIELDS, $fileContents);
  111. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  112. $data = curl_exec($ch);
  113. //print_r($data);exit;
  114. if ($data !== FALSE) {
  115. $data = json_decode($data, TRUE);
  116. } else {
  117. $data = "";
  118. }
  119. $rate_array = array();
  120. if (is_array($data) && sizeof($data['rates']) > 0) {
  121. foreach ($data['rates'] as $key => $value) {
  122. $price = $value['total_price'] * 0.01;
  123. $price_eship_inc = $price + ($price * $increase_rate * 0.01);
  124. $max_delivery_date = "";
  125. if (!empty($value['max_delivery_date'])) {
  126. $max_delivery_date = " (Delivered by: " . $value['max_delivery_date'] . ")";
  127. } else {
  128. $max_delivery_date = "";
  129. }
  130. $np_label_shipping = $value['service_name'] . $max_delivery_date;
  131. if($price_eship_inc!=0)
  132. {
  133. $rate = array(
  134. 'id' => "eShipper_" . $value['service_code'],
  135. 'label' => $np_label_shipping,
  136. 'cost' => $price_eship_inc,
  137. 'calc_tax' => 'per_order'
  138. );
  139. // $this->add_rate($rate);
  140. $rate_array[] = $rate;
  141. //print_r($rate);
  142. }
  143. }
  144. } else {
  145. $rate = array(
  146. 'id' => "eShipper",
  147. 'label' => "Shipping Rates Not Available",
  148. 'cost' => 0.00,
  149. 'calc_tax' => 'per_order'
  150. );
  151. //$this->add_rate($rate);
  152. //print_r($rate);
  153. }
  154.  
  155. $handling = Mage::getStoreConfig('carriers/'.$this->_code.'/handling');
  156. $result = Mage::getModel('shipping/rate_result');
  157. $show = true;
  158. //print_r($rate_array);
  159. if($show){
  160.  
  161.  
  162.  
  163. /*$method->setCarrierTitle($this->getConfigData('title'));
  164. $method->setMethodTitle($this->getConfigData('name'));
  165. $method->setPrice($price);
  166. $method->setCost($price);*/
  167. //echo $this->_code; exit;
  168. $i=0;
  169. foreach($rate_array as $value){
  170. $i++;
  171. $method = Mage::getModel('shipping/rate_result_method');
  172. $method->setCarrier($this->_code);
  173. $method->setMethod($this->_code.$i);
  174. $method->setCarrierTitle($this->getConfigData('title'));
  175. $method->setMethodTitle($value['label']);
  176. $method->setPrice($value['cost']);
  177. $method->setCost($value['cost']);
  178. $result->append($method);
  179. }
  180.  
  181.  
  182.  
  183. }else{
  184. $error = Mage::getModel('shipping/rate_result_error');
  185. $error->setCarrier($this->_code);
  186. $error->setCarrierTitle($this->getConfigData('name'));
  187. $error->setErrorMessage($this->getConfigData('specificerrmsg'));
  188. $result->append($error);
  189. }
  190. return $result;
  191. }
  192. public function getAllowedMethods()
  193. {
  194. return array('excellence'=>$this->getConfigData('name'));
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement