0

How to change this code so that it sends the email notification after successfully payment in WooCommerce?

add_action( 'woocommerce_applied_coupon', 'custom_email_on_applied_coupon', 10, 1 );
function custom_email_on_applied_coupon( $coupon_code ){
    if( $coupon_code == 'mycoupon' ){

        $to = "john@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 );
    }
}
1

The hook you're looking for is "woocommerce payment complete" and it's triggered once an order has been marked as "complete". In the function called by that hook, you need to grab all coupons applied to the order, and check if any of them is the one you're looking for.

add_action( 'woocommerce_payment_complete', 'so_payment_complete' );
function so_payment_complete( $order_id ){

        // Get an instance of WC_Order object
        $order = wc_get_order( $order_id );

        // Coupons used in the order LOOP (as they can be multiple)
        foreach( $order->get_used_coupons() as $coupon_code ){

            // Retrieving the coupon ID
            $coupon_post_obj = get_page_by_title($coupon_code, OBJECT, 'shop_coupon');
            $coupon_id       = $coupon_post_obj->ID;

            // Get an instance of WC_Coupon object in an array(necessary to use WC_Coupon methods)
            $coupon = new WC_Coupon($coupon_id);
            $coupon_code = $coupon->get_code();

            // Check if it's the right coupon
            if ( $coupon_code == 'mycoupon' ){

                $to = "john@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 );

            }

        }
}
| improve this answer | |
  • Thanks for your answer. It works. Just another question: when I replace the The coupon code $coupon_code has been applied by a customer with <p style="text-align: right; direction: rtl;">The coupon code $coupon_code has been applied by a customer</p>, it makes my site inaccessible. why? how can I fix it? – Vahid Damanafshan Oct 14 '19 at 10:54
  • I added my reply as a new answer since it was a long one and needed to use code. Check it out. – alexandrucarjan Oct 15 '19 at 11:02
1

It's because you use double commas without escaping them. You replace this:

    $content = "
    The coupon code $coupon_code has been applied by a customer
    ";

with this:

    $content = "
    The coupon code $coupon_code has been applied by a customer with 
    <p style="text-align: right; direction: rtl;">The coupon code 
    $coupon_code has been applied by a customer</p>
    ";

Notice that double comma from << style=" >> ? That basically closes the first double comma that you used to define the $content variable << $content = " >>. Basically, php sees the $content variable like this:

    $content ="
    The coupon code $coupon_code has been applied by a customer with 
    <p style="

Which means that php will try to interpret this: << text-align: right; direction: rtl; >> as being php code, which will return a fatal error because that's gibberish according to php ( and also, it doesn't see the $content variable line as being ended by ";", which probably means it will return something like "Unexpected 't' on line..."".

You will have to either escape all double commas inside your variable by preceding the escaped character by "\" like so:

    $content = "
    The coupon code $coupon_code has been applied by a customer with 
    <p style=\"text-align: right; direction: rtl;\">The coupon code 
    $coupon_code has been applied by a customer</p>
    ";

or choose single commas:

    $content = "
    The coupon code $coupon_code has been applied by a customer with 
    <p style='text-align: right; direction: rtl;'>The coupon code 
    $coupon_code has been applied by a customer</p>
    ";

I suggest you start getting more familiar with php, which is a pretty easy programming language and you will find a lot of tutorials and documentation. You might also want to read this thread to get a better understanding of the difference between single commas and double commas in php: https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.