Skip to content

Instantly share code, notes, and snippets.

@xadapter
Last active September 16, 2020 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xadapter/5a6404c130dbc61883bda20c392933a4 to your computer and use it in GitHub Desktop.
Save xadapter/5a6404c130dbc61883bda20c392933a4 to your computer and use it in GitHub Desktop.
Snippet to add extra cost per package to the WooCommerce UPS rate. WooCommerce UPS Shipping Plugin with Print Label - https://www.pluginhive.com/product/woocommerce-ups-shipping-plugin-with-print-label/
/**
* Snippet to add extra cost per package to the Woocommerce UPS rate.
* Created at : 12 July 2018
* Updated at : 12 July 2018
* PluginHive Plugins : https://www.pluginhive.com/plugins/
* Gist Link : https://gist.github.com/xadapter/5a6404c130dbc61883bda20c392933a4
*/
add_filter( 'wf_ups_rate', function( $response ) {
$cost_to_add_per_package = 10; // Add fixed cost to shipping rate per package
$total_received_rate_count = count($response->RatedShipment);
$count = 0;
foreach( $response->RatedShipment as $key => $ratedshipment ) {
if( ! empty($response->RatedShipment[$count]) ) {
$package_count = count($ratedshipment->RatedPackage);
$cost_to_be_added = $package_count * $cost_to_add_per_package;
// Negotiated rates
if( ! empty($ratedshipment->NegotiatedRates->NetSummaryCharges->GrandTotal->MonetaryValue) ) {
// Negotiated rate with taxes
if( ! empty($response->RatedShipment[$count]->NegotiatedRates->NetSummaryCharges->TotalChargesWithTaxes->MonetaryValue) ) {
$response->RatedShipment[$count]->NegotiatedRates->NetSummaryCharges->TotalChargesWithTaxes->MonetaryValue += $cost_to_be_added;
}
else{
$response->RatedShipment[$count]->NegotiatedRates->NetSummaryCharges->GrandTotal->MonetaryValue += $cost_to_be_added;
}
}
// Normal rate
else {
$response->RatedShipment[$count]->TotalCharges->MonetaryValue += $cost_to_be_added;
}
}
$count++;
}
return $response;
});
@anilkumarbora
Copy link

not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment