简体   繁体   中英

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 "bob@gmail.com" (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".

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 = "$coupon_code@gmail.com";
        $subject = "Coupon $coupon_code has been applied";
        $content = "
        The coupon code $coupon_code has been applied by a customer
        ";

        wp_mail( $to, $subject, $content );
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM