Search code examples
phpwordpresswoocommercecheckoutcoupon

Send an email notification when a specific coupon code is applied in WooCommerce


I'm looking to setup an affiliate program, starting with the email notifications like when someone applies the coupon code of a an affiliate user.

For example:

  • Coupon code "Bob" applied by a customer.
  • An email notification is sent to "[email protected]" (an affiliate) about the coupon code "bob" applied by a customer in cart or checkout, before order is submitted.

I have tried to use (without any luck): $order->get_used_coupons()

Then once order is completed, an email notification will be sent again: "order was completed with exclusive code".


Solution

  • You can try to use a custom function hooked in woocommerce_applied_coupon action hook (fired each time a coupon is applied).

    Here is just a functional example that will send an email when a coupon is applied (the email address name recipient is set with the coupon name).

    You will need to customize it for your needs:

    add_action( 'woocommerce_applied_coupon', 'custom_email_on_applied_coupon', 10, 1 );
    function custom_email_on_applied_coupon( $coupon_code ){
        if( $coupon_code == 'bob' ){
    
            $to = "[email protected]"; // Recipient
            $subject = sprintf( __('Coupon "%s" has been applied'), $coupon_code );
            $content = sprintf( __('The coupon code "%s" has been applied by a customer'), $coupon_code );
    
            wp_mail( $to, $subject, $content );
        }
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.