Skip to content

Instantly share code, notes, and snippets.

@bdeleasa
Last active August 29, 2015 14:16
Show Gist options
  • Save bdeleasa/1e0dd9c9e9af9797b732 to your computer and use it in GitHub Desktop.
Save bdeleasa/1e0dd9c9e9af9797b732 to your computer and use it in GitHub Desktop.
Initially hide the USPS fallback rate if the user didn't enter a state or postal code.
/**
* Returns either true or false depending on whether the user has entered
* a shipping state or postal code.
*
* @param none
*
* @return bool
*/
function initially_hide_usps_fallback_rate() {
if ( ! WC()->customer->get_shipping_state() || ! WC()->customer->get_shipping_postcode() ) {
return true;
}
else {
return false;
}
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_if_address_not_entered', 999, 2 );
/**
* USPS has an option for a fallback price, but it shows it before the user even
* enters their address. This fixes that by checking whether the user
* has entered any location information. If not, we unset the USPS rate.
*
* @param $rates
* @param $package
*
* @return mixed
*/
function hide_shipping_if_address_not_entered( $rates, $package ) {
if ( initially_hide_usps_fallback_rate() ) {
unset( $rates['usps_fallback'] );
}
return $rates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment