Guest User

Untitled

a guest
Nov 13th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. protected function _getDefaultRate()
  2. {
  3. $helper = Mage::helper('xoom_dicom');
  4. $billingaccount = $this->getConfigData('billingaccount');
  5. $issandbox = $this->getConfigData('issandbox');
  6. $username = $this->getConfigData('username');
  7. $password = Mage::helper('core')->decrypt($this->getConfigData('password'));
  8.  
  9. $Packages = array();
  10.  
  11. $cart = Mage::getModel('checkout/cart')->getQuote();
  12.  
  13. if(Mage::app()->getStore()->isAdmin()){
  14. $cart = Mage::getSingleton('adminhtml/session_quote')->getQuote();
  15. }
  16.  
  17. $totals = Mage::getSingleton('checkout/session')->getQuote()->getTotals(); //Total object
  18. $subtotal = $totals["subtotal"]->getValue();
  19.  
  20. // if($subtotal >= 14000){
  21. // $rate = Mage::getModel('shipping/rate_result_method');
  22.  
  23. // $rate->setCarrier($this->_code);
  24. // $rate->setCarrierTitle($this->getConfigData('title'));
  25. // $rate->setMethod($this->_code);
  26. // $rate->setMethodTitle('Ground');
  27. // $rate->setPrice(0);
  28. // $rate->setCost(0);
  29.  
  30. // return $rate;
  31. // } else {
  32. $freeshipItem = $helper->getFreeshipItemId($cart);
  33. foreach ($cart->getAllVisibleItems() as $item) {
  34. $product = Mage::getModel('catalog/product')->load($item->getProduct()->getId());
  35. $weight = (float)$product->getWeight1();
  36. $Height = (float)$product->getHeight();
  37. $Length = (float)$product->getLength();
  38. $Width = (float)$product->getWidth();
  39. //$weight = $item->getProduct()->getWeight1();
  40. if($weight >($Height*$Width*$Length/166)) {
  41. $CurItemWeight = $weight;
  42. } else {
  43. $CurItemWeight = doubleval(number_format($Height*$Width*$Length/166,2,'.',''));
  44. }
  45. if($subtotal >= 140){
  46. if($freeshipItem && $item->getProduct()->getId()==$freeshipItem){
  47. $CurItemWeight = 0;
  48. $rate = Mage::getModel('shipping/rate_result_method');
  49. $rate->setPrice(0);
  50. $rate->setCost(0);
  51. continue;
  52. }
  53. }
  54. $weight = $CurItemWeight;
  55. $qty = $item->getQty();
  56.  
  57. //-------------------------------
  58. $Packages[] = array('Weight' => 0,'Price'=>0);
  59. for($q=0; $q<$qty; $q++){
  60. $ItemWasAddedToAPackage = false;
  61. foreach($Packages as $PackageId => $PackageData)
  62. {
  63. if($PackageData['Weight']+$CurItemWeight <= $this->MaxPackagesWeight)
  64. {
  65. $Packages[$PackageId]['Weight'] += $CurItemWeight;
  66. //$this->Packages[$PackageId]['Price'] += $Item['Price'];
  67. $ItemWasAddedToAPackage = true;
  68. break;
  69. }
  70. }
  71. if(!$ItemWasAddedToAPackage)
  72. $Packages[] = array('Weight' => $CurItemWeight,'Price'=>$item['Price']);
  73. }
  74. //-------------------------------
  75. $FinalPackages = array();
  76. foreach($Packages as $Package) {
  77. $FinalPackages[] = array(
  78. "parcelType" => 'BX',
  79. "quantity" => 1,
  80. "weight" => $Package['Weight'],
  81. "length" => 1,
  82. "depth" => 1,
  83. "width" => 1
  84. );
  85. }
  86. /* $Packages[] = array(
  87. "parcelType" => 'BX',
  88. "quantity" => $qty,
  89. "weight" => $weight,
  90. "length" => 1,
  91. "depth" => 1,
  92. "width" => 1
  93. ); */
  94. }
  95.  
  96. if(!isset($FinalPackages)){
  97. $rate->setCarrier($this->_code);
  98. $rate->setCarrierTitle($this->getConfigData('title'));
  99. $rate->setMethod($this->_code);
  100. $rate->setMethodTitle('Ground');
  101. return $rate;
  102. }
  103.  
  104. $Request = array(
  105. "deliveryType"=>"GRD",
  106. "paymentType"=>1,
  107. "billingAccount"=>$billingaccount,
  108. "postalCodePickup" => "K0A2M0",
  109. "postalCodeDelivery" => $this->_dest_postcode,
  110. "uom"=>"IN",
  111. "uow"=>"L",
  112. "parcels"=>$FinalPackages
  113. );
  114.  
  115. $url = 'http://api.godicom.com/v0.8/ws/external/rate/estimate/shipment?rate=1';
  116.  
  117. if($issandbox){
  118. $url = 'http://api.godicom.com/sandbox/ws/external/rate/estimate/shipment?rate=1';
  119. }
  120.  
  121. $Headers = array(
  122. "accept: application/json",
  123. "Content-Type: application/json",
  124. "Authorization: Basic ".base64_encode(
  125. $username.
  126. ':'.
  127. $password
  128. ),
  129. "Cache-Control: no-cache"
  130. );
  131.  
  132. $ch = curl_init($url);
  133. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  134. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($Request));
  135. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  136. curl_setopt($ch, CURLOPT_HTTPHEADER, $Headers);
  137.  
  138. $Response = curl_exec($ch);
  139. curl_close($ch);
  140.  
  141. /* echo "<pre>";
  142. var_dump($Request);
  143. var_dump($Response);
  144. echo "</pre>"; */
  145.  
  146. $Response = json_decode($Response,true);
  147. //print_r($Response);
  148. $i = 1;
  149. if(isset($Response['delay'])){
  150. for($d=1;$d<=$Response['delay'];)
  151. {
  152. if(!in_array(date("D",strtotime('today')+($i*86400)),array("Sat","Sun")))
  153. {
  154. if($d == $Response['delay'])
  155. {
  156. $DelayDate = date("Y-m-d",strtotime('today')+($i*86400));
  157. break;
  158. }
  159. $d++;
  160. }
  161. $i++;
  162. }
  163. }
  164.  
  165. if(isset($Response['rates'][0]['total'])){
  166. $rate = Mage::getModel('shipping/rate_result_method');
  167.  
  168. $rate->setCarrier($this->_code);
  169. $rate->setCarrierTitle($this->getConfigData('title'));
  170. $rate->setMethod($this->_code);
  171. $rate->setMethodTitle('Ground');
  172. $rate->setPrice($Response['rates'][0]['total']);
  173. $rate->setCost(0);
  174.  
  175. return $rate;
  176. }else {
  177. return false;
  178. }
  179. // }
  180. }
Add Comment
Please, Sign In to add comment