WooCommerce Enable Shipping Method Based On Coupon

WooCommerce checkout showing a coupon which enables a shipping method

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
WooCommerce Conversion Rate Optimisation (CRO) eBook
100 WooCommerce Conversion Rate Optimisation Tips

Leave a Reply

If you are going to write code in the comments, please wrap it between code tags.

Your email address will not be published. Required fields are marked *

Other Recent Guides

Subscribe To Emails

Get exclusive WooCommerce tips that I only share with email subscribers

Join hundreds of other subscribers