Hello friends, hope you are in good, In today’s post i have explained how to hide other shipping methods when free shipping method is available for checkout.
If various shipping methods are available in your online store and you are providing free shipping after cart total as like 100 USD, or other amount, in this condition below snippets will help you to hide when customer’s cart value is greater than your free shipping limit.
Please make sure to clear your woocommerce cache before adding snippets. Navigate to WooCommerce => Tools => WC Transients => Clear transients.
Copy paste below snippets in your functions.php page of current themes. It is worked with WooCommerce 2.5 version.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /** * woocommerce_package_rates is a 2.1+ hook */ add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 ); /** * Hide shipping rates when free shipping is available * * @param array $rates Array of rates found for the package * @param array $package The package array/object being shipped * @return array of modified rates */ function hide_shipping_when_free_is_available( $rates, $package ) { // Only modify rates if free_shipping is present if ( isset( $rates['free_shipping'] ) ) { // To unset a single rate/method, do the following. This example unsets flat_rate shipping unset( $rates['flat_rate'] ); // To unset all methods except for free_shipping, do the following $free_shipping = $rates['free_shipping']; $rates = array(); $rates['free_shipping'] = $free_shipping; } return $rates; } |
Copy paste below snippets in your functions.php page of current themes. Use below snippets for WooCommerce 2.6+ versions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * Hide shipping rates when free shipping is available. * Updated to support WooCommerce 2.6 Shipping Zones. * * @param array $rates Array of rates found for the package. * @return array */ function my_hide_shipping_when_free_is_available( $rates ) { $free = array(); foreach ( $rates as $rate_id => $rate ) { if ( 'free_shipping' === $rate->method_id ) { $free[ $rate_id ] = $rate; break; } } return ! empty( $free ) ? $free : $rates; } add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 ); |
Do you like & share this article with your friends, and please follow us on Facebook and Twitter for more cool WordPress tutorials.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: ecommerce woocommerce, hide shipping method, shipping, woocommerce, woocommerce shipping method, woocommerce tutorials, wordpress tutorials for beginners