Created
September 22, 2016 04:11
-
-
Save bekarice/4b964c831063c1330ce782a70645bcc9 to your computer and use it in GitHub Desktop.
Show only free / $0 shipping methods when available
This file contains hidden or 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
<?php // only copy if needed | |
/** | |
* Hides any non-free shipping methods if free shipping is available | |
* | |
* @param array $rates array of \WC_Shipping_Rate objects that apply to the cart | |
* @return array - the updated available rates | |
*/ | |
function sww_wc_hide_non_free_shipping( $rates ) { | |
$free_rates = array(); | |
foreach ( $rates as $rate_id => $rate ) { | |
if ( 0 === (int) $rate->cost ) { | |
$free_rates[ $rate_id ] = $rate; | |
// uncomment this `break;` if you only want to show the first free rate | |
// break; | |
} | |
} | |
return ! empty( $free_rates ) ? $free_rates : $rates; | |
} | |
add_filter( 'woocommerce_package_rates', 'sww_wc_hide_non_free_shipping' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment