Created
December 14, 2018 13:34
-
-
Save renjith-ph/36fcbaef72a531f357897580181f0b5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Snippet to adjust rates based on package dimension. | |
* Created at : 14 Dec 2018 | |
* PluginHive Plugins : https://www.pluginhive.com/plugins/ | |
*/ | |
add_filter('wf_ups_rate', 'wf_modify_ups_rate', 10, 2); | |
function wf_modify_ups_rate($xml, $packages){ | |
if(empty($packages)) | |
{ | |
return $xml; | |
} | |
//Config this array with box dimensions and rate to be added. | |
$extra_coast = array( | |
'3,3,0.50' => 10, | |
'10,6,8' => 15, | |
); | |
$request=explode('</AccessRequest>', $packages); | |
$packages = simplexml_load_string(preg_replace('/<\?xml.*\?>/','', $request[1])); | |
if($xml){ | |
foreach ($extra_coast as $extra_coast_dim => $amount_to_add) { | |
$dimension_arrey = explode(",",$extra_coast_dim); | |
foreach ($packages->Shipment->Package as $i => $package) { | |
if(isset($package->Dimensions) && compare_package_dimensions($package->Dimensions, $dimension_arrey) ){ | |
//if negotiated rate | |
if( isset($xml->RatedShipment->NegotiatedRates->NetSummaryCharges->GrandTotal->MonetaryValue) ){ | |
if( property_exists($xml->RatedShipment->NegotiatedRates->NetSummaryCharges, 'TotalChargesWithTaxes') ){ | |
$xml->RatedShipment->NegotiatedRates->NetSummaryCharges->TotalChargesWithTaxes->MonetaryValue += $amount_to_add; | |
}else{ | |
$xml->RatedShipment->NegotiatedRates->NetSummaryCharges->GrandTotal->MonetaryValue += $amount_to_add; | |
} | |
} | |
// if normal rate | |
$xml->RatedShipment->TotalCharges->MonetaryValue += $amount_to_add; | |
} | |
} | |
} | |
} | |
return $xml; | |
} | |
function compare_package_dimensions($package, $dimension_arrey){ | |
$i=0; | |
foreach ($dimension_arrey as $key => &$value) { | |
if( isset($package->Length) && $value == $package->Length ){ | |
$i++; | |
}elseif( isset($package->Width) && $value == $package->Width ){ | |
$i++; | |
}elseif( isset($package->Height) && $value == $package->Height ){ | |
$i++; | |
} | |
} | |
if( $i==3){ | |
return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment