Skip to content

Instantly share code, notes, and snippets.

@adriandmitroca
Last active May 12, 2017 18:32
Show Gist options
  • Select an option

  • Save adriandmitroca/d5f89422873c0093a431cf02e0cac752 to your computer and use it in GitHub Desktop.

Select an option

Save adriandmitroca/d5f89422873c0093a431cf02e0cac752 to your computer and use it in GitHub Desktop.
WooCommerce - Hide shipping methods for specific shipping classes / Add support to hide shipping methods for specified shipping classes to WooCommerce
<?php
add_filter('woocommerce_package_rates', 'hide_shipping_methods', 10, 2);
/**
* Hide specific shipping methods for packages with negative cost set.
* There is no need to hard code any IDs of shipping classes or products if you can just set negative value
* on the back-end and then delete these methods on the fly.
*
* @param $rates
* @param $package
* @return mixed
* @version WooCommerce 3.0+
*/
function hide_shipping_methods($rates, $package) {
foreach ($package['rates'] as $key => $value) {
if ((float)$value->cost < 0) {
unset($rates[$key]);
}
}
return $rates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment