Last active
March 11, 2020 06:57
-
-
Save xadapter/26554e4ec28f48b8599d30810b4dde58 to your computer and use it in GitHub Desktop.
Snippet to adjust UPS shipping rates based on the destination country and state. 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( '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