Skip to content

Instantly share code, notes, and snippets.

@xadapter

xadapter/functions.php

Last active Sep 16, 2020
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@anilkumarbora anilkumarbora commented Sep 16, 2020

not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.