When running promotional campaigns you would find these handy snippets helpful in displaying or hiding specific shipping options to the customer. This code helps to display the free shipping method after applying the specific coupon code.
add_filter( 'woocommerce_package_rates', 'ts_enable_shipping_method_based_on_coupon', 10, 2 ); function ts_enable_shipping_method_based_on_coupon($rates, $package) { global $woocommerce; $coupon_id = 'christmas'; // The specific coupon code if(!in_array($coupon_id, $woocommerce->cart->get_applied_coupons())) { unset( $rates['free_shipping:5'] ); // The shipping method radio button value } return $rates; }
Output
Before the coupon code’christmas’ is applied, the code will hide the ‘Free Shipping’ option.
Once the coupon code ‘christmas’ is applied to the cart page, the ‘free_shipping’ option will be displayed among other shipping options.
Another extended customization that can be done to the above one is to set all shipping methods cost to zero for a free shipping coupon in WooCommerce.