Created
October 5, 2017 18:38
-
-
Save Nishadup/a67c35d913ab8f6a4f15e1944a1025b5 to your computer and use it in GitHub Desktop.
Adjust shipping coast based on country and state
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_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