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.
![How to Enable Shipping Method Based On Coupon in WooCommerce? 1 How to Enable Shipping Method Based On Coupon in WooCommerce? - Tyche Softwares](https://media.tychesoftwares.com/wp-content/uploads/hide-shipping-method-based-on-coupon-1024x644.png)
Once the coupon code ‘christmas’ is applied to the cart page, the ‘free_shipping’ option will be displayed among other shipping options.
![How to Enable Shipping Method Based On Coupon in WooCommerce? 2 How to Enable Shipping Method Based On Coupon in WooCommerce? - Tyche Softwares](https://media.tychesoftwares.com/wp-content/uploads/show-shipping-based-on-coupon-1024x724.png)
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.