Add coupon to email for processing only if the customer has not used one

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

I would like it to be displayed in the mail of the processing order only if the customer has 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>';
}

Thank.

+4
source share
1 answer

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

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 process as 'on-hold'AND 'Processing'statuses :

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 continues in the functions.php file of your active child theme or theme.


:
, :

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

( ), :

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

( ).


:

, if .

, , :

empty( $order->get_used_coupons() )

, "":

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

, ** 'woocommerce_email_before_order_table' ** hook:

:

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>';
}

functions.php

+2

Source: https://habr.com/ru/post/1651182/


All Articles