Last active
February 2, 2025 06:53
-
-
Save faisalahammad/406c77174f73c12e312a972f21fbd71c to your computer and use it in GitHub Desktop.
Hide all paid shipping methods in WooCommerce when free shipping is available. Just drop this code in your functions.php or custom plugin and you're good to go! Perfect for stores that want to encourage free shipping usage.
This file contains 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 | |
add_filter('woocommerce_package_rates', 'hide_shipping_when_free_is_available', 100); | |
function hide_shipping_when_free_is_available($rates) { | |
$free = array(); | |
// Find free shipping methods | |
foreach ($rates as $rate_id => $rate) { | |
if ('free_shipping' === $rate->method_id) { | |
$free[$rate_id] = $rate; | |
break; | |
} | |
} | |
// If free shipping is available, return only free shipping options | |
if (!empty($free)) { | |
return $free; | |
} | |
return $rates; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment