Last active
March 13, 2020 10:28
-
-
Save xadapter/33214869115b885e599c8aa5497dd7e9 to your computer and use it in GitHub Desktop.
Snippet to hide WooCommerce shipping rates when Free shipping is available in the cart. Supports PluginHive Shipping Plugins: https://www.pluginhive.com/product-category/woocommerce-plugin/woocommerce-shipping/
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
add_filter('woocommerce_package_rates', 'xa_hide_shipping_rates_when_free_is_available', 10, 2); | |
function xa_hide_shipping_rates_when_free_is_available($rates, $package) | |
{ | |
global $woocommerce; | |
$version = "2.6"; | |
if (version_compare($woocommerce->version, $version, ">=")) { | |
$new_rates = array(); | |
foreach($rates as $key => $rate) { | |
if ('free_shipping' === $rate->method_id || 'local_pickup' === $rate->method_id) { | |
$new_rates[$key] = $rates[$key]; | |
} | |
} | |
return empty($new_rates) ? $rates : $new_rates; | |
} | |
else { | |
if (isset($rates['free_shipping'])) { | |
// Below code is for unsetting single shipping method/option. | |
$free_shipping = $rates['free_shipping']; | |
if (isset($rates['local_pickup'])) { | |
$local_pickup = $rates['local_pickup']; | |
$rates = array(); // Unset all rates. | |
$rates['local_pickup'] = $local_pickup; // Restore local pickup. | |
$rates['free_shipping'] = $free_shipping; // Restore free shipping rate. | |
} | |
else { | |
$rates = array(); // Unset all rates. | |
$rates['free_shipping'] = $free_shipping; // Restore free shipping rate. | |
} | |
} | |
} | |
return $rates; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment