-
-
Save ChromeOrange/6617327 to your computer and use it in GitHub Desktop.
/** | |
* Hide ALL shipping options when free shipping is available and customer is NOT in certain states | |
* Hide Free Shipping if customer IS in those states | |
* | |
* UPDATED FOR WOOCOMMERCE 2.1 | |
* | |
* Change $excluded_states = array( 'AK','HI','GU','PR' ); to include all the states that DO NOT have free shipping | |
*/ | |
add_filter( 'woocommerce_package_rates', 'hide_all_shipping_when_free_is_available' , 10, 2 ); | |
/** | |
* Hide ALL Shipping option when free shipping is available | |
* | |
* @param array $available_methods | |
*/ | |
function hide_all_shipping_when_free_is_available( $rates, $package ) { | |
$excluded_states = array( 'AK','HI','GU','PR' ); | |
if( isset( $rates['free_shipping'] ) AND !in_array( WC()->customer->shipping_state, $excluded_states ) ) : | |
// Get Free Shipping array into a new array | |
$freeshipping = array(); | |
$freeshipping = $rates['free_shipping']; | |
// Empty the $available_methods array | |
unset( $rates ); | |
// Add Free Shipping back into $avaialble_methods | |
$rates = array(); | |
$rates[] = $freeshipping; | |
endif; | |
if( isset( $rates['free_shipping'] ) AND in_array( WC()->customer->shipping_state, $excluded_states ) ) { | |
// remove free shipping option | |
unset( $rates['free_shipping'] ); | |
} | |
return $rates; | |
} |
Thank you! This is exactly what I need. However, when I add the code to my theme's functions.php file, I get the following error when I enter the checkout process:
Fatal error: Call to a member function get_shipping_state() on a non-object in /[path removed for privacy]/wp-content/themes/Avada/functions.php on line 22
Am I missing something?
What is your line 22 ?
Do I need to add anything to get this filter working? I pasted it in my functions, and I am just trying to get the $available_methods and echo them out in a print_r, but it looks like the filter is not even being hooked. I even tried a simple echo "hello world"; and nothing is getting spit out. I also did a error_log(print_r($available_methods,1)); and that's not outputing anything either. I do have the UPS plugin installed. Any help? I am also on WC 2.1+
I got it. I had to completely clear my cart every time I wanted to test something using that filter and add an item back in. Annoying, but at least I got it.
@aolin480 - THANK YOU. I've been struggling to figure out why the filter wasn't running at all. Clearing out the cart worked like a charm!
I'd like to hide flat_rate when free_shipping is available but I want to keep local delivery available, Can't get it to work. Could you help me with this?
Hi all
Does anyone know how to go about in the array for states in Mexico? Should I write the whole state (eg. Puebla, Chihuahua, Queretaro, etc) or a zip code?
Thanks
The line 29 is wrong. There key in the array must set:
$available_methods['free_shipping'] = $freeshipping;
Otherwise woocommerce do not use the correct shipping method.