gpt4 book ai didi

php - 仅当客户未使用优惠券时,才将优惠券添加到处理订单电子邮件中

转载 作者:可可西里 更新时间:2023-10-31 23:28:26 24 4
gpt4 key购买 nike

我发现了这个将优惠券添加到订单邮件中的片段。

我想让它仅在客户未使用任何优惠券时出现在处理订单邮件中。

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

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


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:

是的,可以很容易地添加带有 2 个条件的 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>';
}

此代码位于您的事件子主题或主题的 function.php 文件中

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

关于php - 仅当客户未使用优惠券时,才将优惠券添加到处理订单电子邮件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38937525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com