Skip to content

Instantly share code, notes, and snippets.

@braddalton
Last active October 11, 2024 16:29
Show Gist options
  • Save braddalton/3e84a93cf4e545624385ed3a6e339ff0 to your computer and use it in GitHub Desktop.
Save braddalton/3e84a93cf4e545624385ed3a6e339ff0 to your computer and use it in GitHub Desktop.
add_filter('woocommerce_package_rates', 'wpsites_remove_flat_rate_methods_over_150', 10, 2);
function wpsites_remove_flat_rate_methods_over_150($rates, $package) {
$cart_total = WC()->cart->get_cart_contents_total();
$threshold = 150;
// Define the shipping method IDs to remove if cart total exceeds $150
$methods_to_remove = array(
'flat_rate:12',
'flat_rate:13',
'flat_rate:14'
);
// Check if the cart total exceeds the threshold
if ($cart_total > $threshold) {
// Loop through the rates and unset the ones to remove
foreach ($rates as $rate_id => $rate) {
if (in_array($rate_id, $methods_to_remove)) {
unset($rates[$rate_id]);
}
}
}
return $rates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment