I'm trying to set it so that for a specific coupon then free shipping is not available.
I then tried this, but it doesn't work
add_filter( 'woocommerce_shipping_packages', function( $packages ) {
$applied_coupons = WC()->session->get( 'applied_coupons', array() );
if ( ! empty( $applied_coupons ) ) {
if (in_array("544", $applied_coupons))
{
$free_shipping_id_11 = 'free_shipping:11';
if($free_shipping_id_11){
unset($packages[0]['rates'][ $free_shipping_id_11 ]);
}
$free_shipping_id_9 = 'free_shipping:9';
if($free_shipping_id_9){
unset($packages[0]['rates'][ $free_shipping_id_9 ]);
}
}
}
return $packages;
} );
Try the following instead:
add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );
function filter_woocommerce_package_rates( $rates, $package ) {
$targeted_coupons = array("544"); // Here set your related coupon codes
$applied_coupons = WC()->cart->get_applied_coupons();
if ( ! empty($applied_coupons) && array_intersect( $targeted_coupons, $applied_coupons ) ) {
foreach ( $rates as $rate_key => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
unset($rates[$rate_key]);
}
}
}
return $rates;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Refresh the shipping caches:
- This code is already saved on your functions.php file.
- In a shipping zone settings, disable / save any shipping method, then enable back / save.
You are done and you can test it.