Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Enable Shipping Method Based On Coupon in WooCommerce?

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? - Tyche Softwares

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? - Tyche Softwares

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.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x