Skip to content

Instantly share code, notes, and snippets.

@reandimo
Created November 3, 2023 14:18
Show Gist options
  • Save reandimo/975d8487629441e16952d92751b47edb to your computer and use it in GitHub Desktop.
Save reandimo/975d8487629441e16952d92751b47edb to your computer and use it in GitHub Desktop.
Hide shipping rates when free shipping is available in Woocommerce's Checkout
<?php
add_filter('woocommerce_package_rates', 'maybe_force_free_shipping', 99);
/**
* @param array $rates Array of rates found for the package.
* @return array
*/
function maybe_force_free_shipping($rates)
{
$free = array();
foreach ($rates as $rate_id => $rate) {
if ($rate->method_id == 'free_shipping') {
$free[$rate_id] = $rate;
break;
}
}
return ! empty($free) ? $free : $rates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment