Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save wbcomdev/40efe00a9dd2c5a781cab5c79b6ed3c6 to your computer and use it in GitHub Desktop.

Select an option

Save wbcomdev/40efe00a9dd2c5a781cab5c79b6ed3c6 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.
*
* Change $excluded_states = array( 'AK','HI','GU','PR' ); to include all the states that DO NOT have free shipping.
*/
function wb_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;
}
add_filter( 'woocommerce_package_rates', 'wb_hide_all_shipping_when_free_is_available', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment