1

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Joe Adroit
  • 25
  • 1
  • 6
  • See what a little google can get you? http://www.remicorson.com/woocommerce-display-coupons-used-in-an-order-in-the-confirmation-email/ – Difster Jul 30 '17 at 00:28
  • As I mention in title, I am looking to add action prior to checkout. – Joe Adroit Jul 30 '17 at 01:36

1 Answers1

2

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Exactly what I was looking for. For future reference, how do you hunt down WC hooks? docs.woocommerce.com? Thanks! – Joe Adroit Jul 30 '17 at 14:50
  • @JoeAdroit In the source code of `WC_Cart add_discount()` method… – LoicTheAztec Jul 30 '17 at 15:02
  • I see it now in `WC_Cart`. Appreciated – Joe Adroit Jul 30 '17 at 15:11
  • How do i get the email id in the form i mean when i am setting in the admin in woocommerce section, and it should be saved it db againts coupon code bob – indianwebdevil Sep 01 '18 at 19:55
  • Very simple, lets say there are 3 affiliates, a1@gmail.com, a2@gmail.com, a3@gmail.com and to add these affilates i add 3 coupons when adding info of the coupon i get coupon, description likewise i should have 'Notification email when applied' where i ll enter a1@gmail.com for coupon coupon_a1, and a2@gmail.com for coupon coupon_a2 and so on. Hope I ll get an answer. – indianwebdevil Sep 01 '18 at 20:17
  • @LoicTheAztec hope the comment helps – indianwebdevil Sep 01 '18 at 20:37
  • @nepsdotin You just need 3 if statement, one for each coupon code / email pairs … or not? – LoicTheAztec Sep 01 '18 at 20:38
  • @LoicTheAztec I ll later add one more affilate, so I can't change in the code everytime, I should have a way to collect it in a textbox and store in table, which table should i use, for editing view, which file should i modify. Your guidance would help – indianwebdevil Sep 01 '18 at 20:45
  • @nepsdotin The best way should be to add in coupons edit pages some related custom fields, which values will be easily accessible in `woocommerce_applied_coupon` hook from the coupon code or the coupon post ID as coupon meta data. Sorry but I am exhausted and I go to bed. All the best. – LoicTheAztec Sep 01 '18 at 21:04
  • @LoicTheAztec Thanks Good night :) – indianwebdevil Sep 01 '18 at 21:13
  • @LoicTheAztec Could you please tell me what do "10" and "1" do in your code? And could you please answer this question? https://stackoverflow.com/questions/58355033 – Vahid Damanafshan Oct 12 '19 at 14:16