4

I came across this snippet that adds a coupon to the order mail.

I would like to make it appear in the processing order mail only if the customer have not used any coupon.

add_action( 'woocommerce_email_before_order_table', 'add_content', 20 );

function add_content() {
echo '<h2 id="h2thanks">Get 20% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
}

Thanks.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Volt
  • 113
  • 6

1 Answers1

2

@update 2: New orders are most of the time in 'on-hold' status, but not in 'Processing'.
In this case, use this instead:

add_action( 'woocommerce_email_before_order_table', 'processing_order_mail_message', 20 );
function processing_order_mail_message( $order ) {
    if ( empty( $order->get_used_coupons() ) && $order->post_status == 'wc-on-hold' )
        echo '<h2 id="h2thanks">Get 20% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
}

Or to handle both 'on-hold' AND 'processing' statuses use this:

add_action( 'woocommerce_email_before_order_table', 'processing_order_mail_message', 20 );
function processing_order_mail_message( $order ) {
    if ( empty( $order->get_used_coupons() ) && ( $order->post_status == 'wc-on-hold' || $order->post_status == 'wc-processing' ) )
        echo '<h2 id="h2thanks">Get 20% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
}

This code goes on function.php file of your active child theme or theme

This code is tested and fully functional


TESTING:
To display the status of the emailed order you can add in the function this line:

echo '<p>The status of this order is: ' . $order->post_status . '</p>';

To display the coupons used (if they are some) add this:

echo '<p>Coupons used in this order are: '; print_r( $order->get_used_coupons() ); echo '</p>'

(this is just for testing purpose).


Original answer:

Yes it is possible easily adding an if statement with 2 conditions.

The first one detect if a coupon has not been used in the order used in the order:

empty( $order->get_used_coupons() )

And the second one will detect if the order is in "processing" status:

$order->post_status == 'wc-processing'

If that conditions are matched, your message is displayed using**'woocommerce_email_before_order_table'** hook:

Here is your customized code snippet:

add_action( 'woocommerce_email_before_order_table', 'processing_order_mail_message', 20 );
function processing_order_mail_message( $order ) {
    if ( empty( $order->get_used_coupons() ) && $order->post_status == 'wc-processing' )
        echo '<h2 id="h2thanks">Get 20% off</h2><p id="pthanks">Thank you for making this purchase! Come back and use the code "<strong>Back4More</strong>" to receive a 20% discount on your next purchase! Click here to continue shopping.</p>';
}

This code goes on function.php file of your active child theme or theme

This code is tested and fully functional

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • However, I tested it with a coupon and I got an email order with the following text "Fatal error: Call to a member function get_used_coupons() on string in C:\xampp\....\Divi\functions.php on line 8527" – Volt Aug 14 '16 at 10:16
  • Sorry, now there is no error but there is no coupon in the email order either. I tried as many ways as I could come up with ... – Volt Aug 14 '16 at 23:38
  • I posted another thread regarding this issue asking how to display that coupon only in completed order mail. [link](http://stackoverflow.com/questions/38975742/displaying-a-coupon-only-in-completed-order-email) – Volt Aug 16 '16 at 13:08