Last active
September 16, 2020 16:56
-
-
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/
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 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; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not working