Add coupon to the processing order email only if t

2019-04-10 11:50发布

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.

1条回答
霸刀☆藐视天下
2楼-- · 2019-04-10 12:18

@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

查看更多
登录 后发表回答