Created
April 24, 2020 12:02
-
-
Save PluginHive/6663f8aa7cfa993a9c803141a59c5928 to your computer and use it in GitHub Desktop.
Code snippet to Add extra costs based on box sizes using WooCommerce UPS Shipping Plugin: https://www.pluginhive.com/product/woocommerce-ups-shipping-plugin-with-print-label/
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
add_filter('wf_ups_rate', 'wf_modify_ups_rate', 10, 2); | |
function wf_modify_ups_rate($xml, $packages){ | |
//Config this array with box dimensions and rate to be added. | |
$extra_coast = array( | |
'7,9,8' => 10, | |
'10,6,8' => 15, | |
); | |
if($xml){ | |
foreach ($extra_coast as $extra_coast_dim => $amount_to_add) { | |
$dimension_arrey = explode(",",$extra_coast_dim); | |
foreach ($packages as $i => $package) { | |
if( compare_package_dimensions($package['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){ | |
foreach ($dimension_arrey as $key => &$value) { | |
if( isset($package['Length']) && $value == $package['Length'] ){ | |
unset($package['Length']); | |
}elseif( isset($package['Width']) && $value == $package['Width'] ){ | |
unset($package['Width']); | |
}elseif( isset($package['Height']) && $value == $package['Height'] ){ | |
unset($package['Height']); | |
} | |
} | |
if( empty($package['Length']) && empty($package['Width']) && empty($package['Height']) ){ | |
return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment