Skip to content

How to Send an Email When a Coupon Is Used in WooCommerce

By geekerhub 6 min read

WooCommerce email notification when a customer uses a coupon code

WooCommerce allows customers to apply coupon codes during checkout, but store owners do not receive a dedicated email notification whenever a particular coupon is used in an order. This can make it difficult to track promotional campaigns, affiliate discounts, special offers, or coupon-based marketing activities.

If you want to know whenever a customer uses a specific coupon code, you can automate the process with a simple PHP snippet. WooCommerce provides order hooks that allow you to check which coupons were applied and send an email notification automatically.

In this tutorial, you will learn how to send an email notification when a specific WooCommerce coupon is used. You can customize the coupon code, recipient email address, subject, and message without installing an additional plugin.

What This WooCommerce Snippet Does

  • Detects coupon codes applied to WooCommerce orders.
  • Sends an email notification when a specific coupon is used.
  • Allows you to customize the notification recipient.
  • Includes order details in the email message.
  • Supports multiple coupon codes.
  • Prevents duplicate notifications for the same order.
  • Works without additional WooCommerce plugins.

WooCommerce PHP Snippet: Send Email When Coupon Is Used

The following PHP snippet checks the coupons applied to an order when its status changes to Completed. If the specified coupon code is found, WooCommerce sends an email notification to your chosen email address.

Add the following code to your child theme’s functions.php file or use a code snippets plugin.

<?php
/**
 * Send Email When a Specific WooCommerce Coupon Is Used
 */

add_action(
    'woocommerce_order_status_completed',
    'geekerhub_email_when_coupon_used'
);

function geekerhub_email_when_coupon_used( $order_id ) {

    $order = wc_get_order( $order_id );

    if ( ! $order ) {
        return;
    }

    // Prevent duplicate notification emails.
    if ( $order->get_meta( '_coupon_notification_sent' ) ) {
        return;
    }

    // Coupon code to monitor.
    $target_coupon = 'xyz123';

    // Get coupons applied to the order.
    $used_coupons = $order->get_coupon_codes();

    $coupon_found = false;

    foreach ( $used_coupons as $coupon_code ) {

        if ( strtolower( $coupon_code ) === strtolower( $target_coupon ) ) {
            $coupon_found = true;
            break;
        }
    }

    if ( ! $coupon_found ) {
        return;
    }

    // Notification recipient.
    $to = 'your@email.com';

    // Email subject.
    $subject = 'Coupon Used in Order #' . $order_id;

    // Prepare email message.
    $message  = "A customer has used your coupon code.\n\n";
    $message .= "Order ID: " . $order_id . "\n";
    $message .= "Coupon: " . $target_coupon . "\n";
    $message .= "Customer: " . $order->get_billing_first_name() . "\n";
    $message .= "Email: " . $order->get_billing_email() . "\n";
    $message .= "Order Total: " . $order->get_formatted_order_total() . "\n";

    // Send notification.
    $sent = wp_mail( $to, $subject, $message );

    // Save notification status only if email was accepted.
    if ( $sent ) {
        $order->update_meta_data( '_coupon_notification_sent', 'yes' );
        $order->save();
    }
}

How This WooCommerce Coupon Email Snippet Works

The snippet uses the woocommerce_order_status_completed action hook. This hook runs when an order changes to the Completed status.

Here is what happens when the code executes:

  1. Order verification: WooCommerce retrieves the order using its order ID.
  2. Duplicate check: The code checks whether a notification has already been sent for that order.
  3. Coupon detection: It retrieves the coupon codes applied to the order.
  4. Coupon comparison: The code compares each coupon against your specified coupon code.
  5. Email notification: If a matching coupon is found, WordPress sends an email using wp_mail().
  6. Save notification status: The order is updated to prevent duplicate notification emails.

For example, if your monitored coupon code is xyz123, the notification will be sent when an order containing that coupon reaches the Completed status.

How to Change the Coupon Code

You can monitor any coupon created in your WooCommerce store. Find the following line in the snippet:

$target_coupon= 'xyz123';

Replace xyz123 with your actual coupon code.

For example, if your coupon code is SUMMER20, change it to:

$target_coupon= 'SUMMER20';

The comparison ignores uppercase and lowercase differences, so both summer20 and SUMMER20 will match.

How to Send Coupon Notifications to a Different Email Address

By default, the example sends notifications to the email address specified in the $to variable.

Find this line:

$to= 'your@email.com';

Replace it with your preferred email address:

$to= 'admin@example.com';

You can also send notifications to multiple recipients by separating their email addresses with commas.

$to= 'admin@example.com, manager@example.com';

How to Monitor Multiple Coupon Codes

If your store uses multiple promotional coupons, you can modify the coupon detection logic to monitor several codes instead of just one.

Replace the existing coupon comparison section with the following code:

// Coupon codes to monitor.
$target_coupons = array(
    'xyz123',
    'SUMMER20',
    'WELCOME10'
);

$used_coupons = array_map(
    'strtolower',
    $order->get_coupon_codes()
);

$target_coupons = array_map(
    'strtolower',
    $target_coupons
);

$coupon_found = ! empty(
    array_intersect( $used_coupons, $target_coupons )
);

if ( ! $coupon_found ) {
    return;
}

Now, WooCommerce will send the notification if any of the specified coupon codes are applied to the order.

Make sure you replace the original coupon detection section rather than adding this code alongside it.

Can You Send an Email Before the Order Is Completed?

Yes. The example above sends the notification when an order reaches the Completed status. If you want to send an email earlier, you can use a different WooCommerce order hook.

For example, the woocommerce_checkout_order_processed action runs after checkout creates an order. It can be useful when you need to detect coupon usage immediately after checkout rather than waiting for order completion.

However, remember that orders may still be pending payment or subsequently cancelled. Choose the hook according to when you actually want the notification to be triggered.

Where Should You Add This Code?

You can add the PHP snippet using either of these methods:

  • Child theme: Add the code to your child theme’s functions.php file.
  • Code Snippets plugin: Create a new PHP snippet and activate it.

A code snippets plugin is a convenient option because your custom code remains separate from the theme files.

Before using the snippet on a live store, test it with a staging website. Place a test order using the selected coupon and change its status to Completed to verify that the notification works correctly.

Frequently Asked Questions

Can WooCommerce send an email whenever a coupon is used?

Yes. You can use WooCommerce order hooks to detect applied coupon codes and send custom email notifications using the WordPress wp_mail() function.

Can I send an email for every coupon used?

Yes. You can remove the specific coupon comparison and trigger the email whenever an order contains at least one applied coupon.

Can I send coupon notifications to multiple email addresses?

Yes. Add multiple comma-separated email addresses to the recipient variable in the PHP snippet.

Does this snippet support multiple coupons in one order?

Yes. The code checks all coupon codes applied to an order. The multiple-coupon example also allows you to monitor several different promotional codes.

Why am I not receiving coupon notification emails?

Check that the order has reached the required status, the coupon code matches, and the recipient email address is correct. Also verify that your WordPress email delivery configuration is working. You may need an SMTP configuration to improve email deliverability.

Does this require an additional WooCommerce plugin?

No. The functionality uses standard WooCommerce order hooks and the WordPress email function. No separate coupon notification plugin is required.

Final Thoughts

Sending an email when a WooCommerce coupon is used is a useful way to monitor promotional campaigns, affiliate codes, and special discounts.

With a small PHP snippet, you can automatically check coupon usage and notify yourself whenever a matching order is completed. You can also customize the recipient, monitor multiple coupon codes, and include additional order information in the notification.

If you need more control over coupon activity in your WooCommerce store, this is a simple starting point without adding another plugin.