仅当客户未使用优惠券时,才将优惠券添加到处理订单电子邮件中 [英] Add coupon to the processing order email only if the customer have not used one

查看:138
本文介绍了仅当客户未使用优惠券时,才将优惠券添加到处理订单电子邮件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到了一个向订单邮件添加优惠券的代码段.

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

谢谢.

推荐答案

@update 2:新订单大部分时间处于 'on-hold'状态,而不是'Processing'.
在这种情况下,请改用此方法:

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

或者要处理'on-hold''processing'两种状态,请使用以下命令:

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

此代码位于活动子主题或主题的function.php文件上

此代码已经过测试且功能齐全

测试:
要显示电子邮件订单的状态,您可以在此行中添加功能:

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

(这只是出于测试目的).


原始答案:

是的,很容易添加具有两个条件的 if语句.

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'

如果符合条件,则使用** 'woocommerce_email_before_order_table' **钩子显示您的消息:

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

此代码位于活动子主题或主题的function.php文件上

此代码已经过测试且功能齐全

这篇关于仅当客户未使用优惠券时,才将优惠券添加到处理订单电子邮件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆