Skip to content

Instantly share code, notes, and snippets.

@Nishadup
Created October 5, 2017 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Nishadup/a67c35d913ab8f6a4f15e1944a1025b5 to your computer and use it in GitHub Desktop.
Save Nishadup/a67c35d913ab8f6a4f15e1944a1025b5 to your computer and use it in GitHub Desktop.
Adjust shipping coast based on country and state
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_woocommerce_shipping_pro'); //Set methods to adjust
//Config this array with country code, state code and rate to be added.
$destination_array = array(
'US' => array(
'NY' => 5,
'CA' => 6,
),
'CA' => array(
'*' => 10,
)
);
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