Skip to content

Instantly share code, notes, and snippets.

@xadapter xadapter/functions.php
Last active Jan 19, 2018

Embed
What would you like to do?
UPS Shipping Rate Adjustment based on Destination Country and state https://www.xadapter.com/product/woocommerce-ups-shipping-plugin-with-print-label/
add_filter( 'woocommerce_package_rates', 'wf_add_charge_if_exceed_cost', 15, 2 );
function wf_add_charge_if_exceed_cost( $available_shipping_methods, $package ){
$mothods = array('wf_shipping_ups:07','wf_shipping_ups:08'); //Set methods to adjust
//Config this array with country code, state code and rate to be added.
$destination_array = array(
'US' => array(
'NY' => -0.05,
'CA' => -0.06,
),
'CA' => array(
'*' => 0.1,
)
);
global $woocommerce;
$customer_country = $woocommerce->customer->get_shipping_country();
$customer_state = !empty($destination_array[$customer_country]['*']) ? '*' : $package['destination']['state'];
foreach($mothods as &$current_method) {
foreach ($available_shipping_methods as $shipping_method => $value) {
if( strpos( $shipping_method, $current_method ) !== false ) {
// Cost adjustment
if ( ! empty( $destination_array[$customer_country][$customer_state] ) ) {
$value->cost += $value->cost * floatval( $destination_array[$customer_country][$customer_state] );
}
}
}
}
return $available_shipping_methods;
}
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.