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

泄露秘密 提交于 2019-12-08 08:05:47

问题


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".


回答1:


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 = "jack.hoover@gmail.com"; // 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.



来源:https://stackoverflow.com/questions/45395261/send-an-email-notification-when-a-specific-coupon-code-is-applied-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!