Checkout

WooCommerce Enable Shipping Method Based On Coupon

Pre-Requisites

There are no pre-requisites in order for you to be able to implement this solution.

How To Implement This Solution?

Simply copy the pre-coded solution to your active theme’s functions.php or preferably the Code Snippets Plugin.

About This Solution

This guide shows you how to hide a specific shipping method option and only enable it when a specific coupon code has been entered. For this to work properly, you will need to tweak the line 10 in the code – the specific coupon code to enable the hidden shipping method option, and line 13 in the code – the shipping method radio option value. You can get the shipping method radio option value by first going to the checkout page, using inspect element, clicking the radio button of the shipping method, then expanding the li class. It will look something like “local_pickup:13”. You’ll also need to clear all customer sessions under Dashboard > WooCommerce > Status > Tools. This tool will empty all active customer baskets so it’s probably best to do this when the site is quiet. This is a very niche solution but some parts of the code could help you implement a similar solution your business requires.

/**
 * Snippet Name:	WooCommerce enable shipping method if a specific coupon has been entered
 * Snippet Author:	ecommercehints.com
 */

add_filter( 'woocommerce_package_rates', 'ecommercehints_enable_shipping_method_based_on_coupon', 10, 2 );

function ecommercehints_enable_shipping_method_based_on_coupon($rates, $package) {
global $woocommerce;
$coupon_id = 'showlocalpickup'; // The specific coupon code

if(!in_array($coupon_id, $woocommerce->cart->get_applied_coupons())) {
     unset( $rates['local_pickup:13'] ); // The shipping method radio button value
}
return $rates;
}

How Can I Get The Shipping Method Radio Button Value?

Use your browser’s developer tools to inspect the radio button and get the value like shown in the image.

WooCommerce get shipping method radio button value

100 WooCommerce Conversion Rate Optimisation Tips

This field is for validation purposes and should be left unchanged.

Let’s collaborate!

Need to outsource WordPress development?

Join forces with UnlimitedWP for an outsourced white label web development service you can truly rely on.

First Timer Here?

Sign up to receive 10% off on your first month with us.

26027
WELCOME OFFER