-
-
Save xadapter/a64ca03e2016ae66809c8f58a8cc2247 to your computer and use it in GitHub Desktop.
Snippet to adjust WooCommerce shipping cost based on the country and state. Supports PluginHive Shipping Plugins: https://www.pluginhive.com/product-category/woocommerce-plugin/woocommerce-shipping/
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