How to automatically apply a coupon code in WooCommerce using PHP.

In this tutorial, we write a small PHP function that will automatically add a specific coupon code to the user’s cart during checkout.


For the most recent Black Friday sales, I had a client who needed to add a coupon code to a customer’s cart automatically. The client found a plugin that did this, but it used a method that auto-generated a new coupon for each customer who visited the site. As you can imagine, this put a big performance load on the server.

The original goal was to reduce friction for the customer. Unfortunately, it did the opposite. Friday morning, at the height of the sale, Customer Service began receiving reports that the coupon codes were failing. This is when I received the urgent call and jumped into action.

After a few minutes of triaging the issue and confirming with the client what they were trying to accomplish, I proposed using the default WooCommerce coupon codes and writing a custom function to add it to the user’s cart automatically.

For those looking for the code, below is the final output. If you’re looking for instruction, please read on.

function kindling_auto_apply_coupon() {
    $coupon_code = 'YOUR_COUPON_CODE'; // Replace with your coupon code

    if ( ! WC()->cart->has_discount( $coupon_code ) ) {
        WC()->cart->apply_coupon( $coupon_code );
        wc_print_notices();
    }
}

add_action( 'woocommerce_before_cart', 'kindling_auto_apply_coupon' );

Instructions

  1. Ensure you have the coupon code you want to use created.
  2. Add the PHP function above to your theme. This is typically in your functions.php file. If you’re using Kindling, I put this in the inc/woocommerce.php file.
  3. Replace YOUR_COUPON_CODE with the coupon code from step 1.
  4. Test by adding a product to your cart and then visiting the cart or checkout page to confirm that the coupon code is automatically applied.

Explanation of the code

The kindling_auto_apply_coupon function automatically applies a specified coupon code to the WooCommerce cart if it’s not already applied. The function is executed before the cart page is rendered, ensuring the coupon is applied whenever users visit their cart.

Function Declaration

function kindling_auto_apply_coupon() {

This line declares a new function named kindling_auto_apply_coupon. This is a custom function, and the name is chosen to be unique to avoid conflicts with other functions in WordPress or WooCommerce. The theme I use is Kindling, so I prefix my functions with kindling. Feel free to customize this name to your liking.

Setting the Coupon Code

$coupon_code = 'YOUR_COUPON_CODE';

Here, a variable $coupon_code is declared and assigned a string value 'YOUR_COUPON_CODE'. This string should be replaced with the actual coupon code you intend to apply automatically.

The coupon code must exist in your WooCommerce settings. Otherwise, the user will receive a “Coupon not found.” error message.

Checking if the Coupon is Already Applied

if ( ! WC()->cart->has_discount( $coupon_code ) ) {

This line is a conditional statement (an if statement) that checks whether the coupon code has already been applied to the cart:

  • WC() is a function that provides access to the WooCommerce instance.
  • WC()->cart accesses the cart object of the current user’s session.
  • has_discount( $coupon_code ) is a method of the cart object that checks if the specified coupon code has already been applied to the cart.
  • The ! operator negates the condition, so the if statement checks if the coupon code is not already applied.

Applying the Coupon Code

WC()->cart->apply_coupon( $coupon_code ); wc_print_notices();

If the coupon code is not already applied, these lines of code will execute:

  • apply_coupon( $coupon_code ) is a method that applies the specified coupon code to the cart.
  • wc_print_notices() is a WooCommerce function that displays any notices to the user, such as a success message for applying the coupon or an error message if the coupon can’t be applied.

Adding the Function to a WooCommerce Hook

add_action( 'woocommerce_before_cart', 'kindling_auto_apply_coupon' );

This line uses the WordPress add_action function to hook the custom function kindling_auto_apply_coupon to a specific action in WooCommerce:

  • woocommerce_before_cart is the action hook that triggers before the WooCommerce cart is rendered. This means the function kindling_auto_apply_coupon will be called every time the cart page is loaded.
  • kindling_auto_apply_coupon is the name of the function that will be executed when the hook is triggered.

Summary

So there you have it, a quick little function that can keep your customers happy and your checkout flowing. Let me know if you use this function or expand it to meet your needs.