4

我遇到了这个在订单邮件中添加优惠券的片段。

只有当客户没有使用任何优惠券时,我才会让它出现在处理订单邮件中。

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

谢谢。

4

1 回答 1

2

@update 2:新订单大部分时间都处于'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>';
}

或者要同时处理'on-hold'AND'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' || $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 文件中

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


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

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带有 2 个条件的语句

第一个检测是否在订单中使用的订单中没有使用优惠券:

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 文件中

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

于 2016-08-14T00:00:50.017 回答