In the WooCommerce default template, above the shopping cart and checkout page, there is a form that allows the user to enter a coupon, and from a user experience point of view, the coupon form willdecentralizedThe attention of the potential customer, so that they abandon the checkout to look for a discount code.
However, offering discounts is still an essential and important strategy to encourage purchases and reward loyal customers, and the best compromise is toUse coupons when customers visit specific URLs. By doing so, we can streamline the checkout process, reduce friction, and ultimately increase conversion rates.
In this tutorial, we'll learn how to set up automatic coupon application in WooCommerce to ensure a seamless and efficient shopping checkout experience for you and your customers.
In the above screenshot, there is a parameter "code" in the URL, the value of which is the coupon code. Using the code snippet below, the coupon code can be automatically applied to the shopping cart without any manual action by the user!
Programmatically apply WooCommerce coupons based on URL parameters
There are two things to keep in mind.
First, we have to determine the URL parameter to be used. In the snippet below, we have used 'code', of course, you can change it as much as you want, and then we need to include the coupon to this parameter from, for example:example.com/?code=xyz
Of course, we need to create it before we can add the coupon to the URL parameters. Depending on the needs of the marketing strategy, we need to consider whether the coupon code will use a random code or a meaningful code. After creating the coupon, if you don't want people to add multiple coupons to the cart with different URLs, be sure to check the Coupon Usage Restriction setting in the"Personal use only"The checkbox ("If the coupon cannot be combined with other couponsTo use, please check theelectivecheckbox").
Other than that, the code below is divided into two parts and the results are the same. The only difference is that one function will work when the cart is not empty(wprs_add_coupon_to_session)), and another function that will trigger when you add to the cart(wprs_add_coupon_to_cart)).
function wprs_add_coupon_to_session()
{
if (empty($_GET['code'])) return; if ( !
if ( ! WC()->session || (WC()->session && ! WC()->session->has_session())) {
WC()->session->set_customer_session_cookie(true);
}
$coupon_code = esc_attr($_GET['code']);
WC()->session->set('coupon_code', $coupon_code);
if (WC()->cart && ! WC()->cart->has_discount($coupon_code)) {
WC()->cart->calculate_totals();
WC()->cart->add_discount($coupon_code);
WC()->session->__unset('coupon_code');
}
}
add_action('woocommerce_add_to_cart', 'wprs_add_coupon_to_cart');
function wprs_add_coupon_to_cart()
{
$coupon_code = WC()->session ? WC()->session->get('coupon_code') : false;
if ( ! $coupon_code || empty($coupon_code)) return;
if (WC()->cart && ! WC()->cart->has_discount($coupon_code)) {
WC()->cart->calculate_totals();
WC()->cart->add_discount($coupon_code);
WC()->session->__unset('coupon_code');
}
}
Add the above code to the functions.php of the theme or the appropriate location of the plugin, you can realize the function of automatically applying coupons through the URL, if you have a better way to achieve this, please feel free to share it in the comments, we will discuss it together.