Skip to content

Instantly share code, notes, and snippets.

@ChromeOrange
Last active March 7, 2016 19:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChromeOrange/6617327 to your computer and use it in GitHub Desktop.
Save ChromeOrange/6617327 to your computer and use it in GitHub Desktop.
Show only free shipping in all states except exclusion list, hide free shipping if the customer is in one of those states
/**
* 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;
}
@camposricardo86
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment