Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save faisalahammad/406c77174f73c12e312a972f21fbd71c to your computer and use it in GitHub Desktop.
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.
<?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