Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected function _getDefaultRate()
- {
- $helper = Mage::helper('xoom_dicom');
- $billingaccount = $this->getConfigData('billingaccount');
- $issandbox = $this->getConfigData('issandbox');
- $username = $this->getConfigData('username');
- $password = Mage::helper('core')->decrypt($this->getConfigData('password'));
- $Packages = array();
- $cart = Mage::getModel('checkout/cart')->getQuote();
- if(Mage::app()->getStore()->isAdmin()){
- $cart = Mage::getSingleton('adminhtml/session_quote')->getQuote();
- }
- $totals = Mage::getSingleton('checkout/session')->getQuote()->getTotals(); //Total object
- $subtotal = $totals["subtotal"]->getValue();
- // if($subtotal >= 14000){
- // $rate = Mage::getModel('shipping/rate_result_method');
- // $rate->setCarrier($this->_code);
- // $rate->setCarrierTitle($this->getConfigData('title'));
- // $rate->setMethod($this->_code);
- // $rate->setMethodTitle('Ground');
- // $rate->setPrice(0);
- // $rate->setCost(0);
- // return $rate;
- // } else {
- $freeshipItem = $helper->getFreeshipItemId($cart);
- foreach ($cart->getAllVisibleItems() as $item) {
- $product = Mage::getModel('catalog/product')->load($item->getProduct()->getId());
- $weight = (float)$product->getWeight1();
- $Height = (float)$product->getHeight();
- $Length = (float)$product->getLength();
- $Width = (float)$product->getWidth();
- //$weight = $item->getProduct()->getWeight1();
- if($weight >($Height*$Width*$Length/166)) {
- $CurItemWeight = $weight;
- } else {
- $CurItemWeight = doubleval(number_format($Height*$Width*$Length/166,2,'.',''));
- }
- if($subtotal >= 140){
- if($freeshipItem && $item->getProduct()->getId()==$freeshipItem){
- $CurItemWeight = 0;
- $rate = Mage::getModel('shipping/rate_result_method');
- $rate->setPrice(0);
- $rate->setCost(0);
- continue;
- }
- }
- $weight = $CurItemWeight;
- $qty = $item->getQty();
- //-------------------------------
- $Packages[] = array('Weight' => 0,'Price'=>0);
- for($q=0; $q<$qty; $q++){
- $ItemWasAddedToAPackage = false;
- foreach($Packages as $PackageId => $PackageData)
- {
- if($PackageData['Weight']+$CurItemWeight <= $this->MaxPackagesWeight)
- {
- $Packages[$PackageId]['Weight'] += $CurItemWeight;
- //$this->Packages[$PackageId]['Price'] += $Item['Price'];
- $ItemWasAddedToAPackage = true;
- break;
- }
- }
- if(!$ItemWasAddedToAPackage)
- $Packages[] = array('Weight' => $CurItemWeight,'Price'=>$item['Price']);
- }
- //-------------------------------
- $FinalPackages = array();
- foreach($Packages as $Package) {
- $FinalPackages[] = array(
- "parcelType" => 'BX',
- "quantity" => 1,
- "weight" => $Package['Weight'],
- "length" => 1,
- "depth" => 1,
- "width" => 1
- );
- }
- /* $Packages[] = array(
- "parcelType" => 'BX',
- "quantity" => $qty,
- "weight" => $weight,
- "length" => 1,
- "depth" => 1,
- "width" => 1
- ); */
- }
- if(!isset($FinalPackages)){
- $rate->setCarrier($this->_code);
- $rate->setCarrierTitle($this->getConfigData('title'));
- $rate->setMethod($this->_code);
- $rate->setMethodTitle('Ground');
- return $rate;
- }
- $Request = array(
- "deliveryType"=>"GRD",
- "paymentType"=>1,
- "billingAccount"=>$billingaccount,
- "postalCodePickup" => "K0A2M0",
- "postalCodeDelivery" => $this->_dest_postcode,
- "uom"=>"IN",
- "uow"=>"L",
- "parcels"=>$FinalPackages
- );
- $url = 'http://api.godicom.com/v0.8/ws/external/rate/estimate/shipment?rate=1';
- if($issandbox){
- $url = 'http://api.godicom.com/sandbox/ws/external/rate/estimate/shipment?rate=1';
- }
- $Headers = array(
- "accept: application/json",
- "Content-Type: application/json",
- "Authorization: Basic ".base64_encode(
- $username.
- ':'.
- $password
- ),
- "Cache-Control: no-cache"
- );
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($Request));
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $Headers);
- $Response = curl_exec($ch);
- curl_close($ch);
- /* echo "<pre>";
- var_dump($Request);
- var_dump($Response);
- echo "</pre>"; */
- $Response = json_decode($Response,true);
- //print_r($Response);
- $i = 1;
- if(isset($Response['delay'])){
- for($d=1;$d<=$Response['delay'];)
- {
- if(!in_array(date("D",strtotime('today')+($i*86400)),array("Sat","Sun")))
- {
- if($d == $Response['delay'])
- {
- $DelayDate = date("Y-m-d",strtotime('today')+($i*86400));
- break;
- }
- $d++;
- }
- $i++;
- }
- }
- if(isset($Response['rates'][0]['total'])){
- $rate = Mage::getModel('shipping/rate_result_method');
- $rate->setCarrier($this->_code);
- $rate->setCarrierTitle($this->getConfigData('title'));
- $rate->setMethod($this->_code);
- $rate->setMethodTitle('Ground');
- $rate->setPrice($Response['rates'][0]['total']);
- $rate->setCost(0);
- return $rate;
- }else {
- return false;
- }
- // }
- }
Add Comment
Please, Sign In to add comment