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

时间:2016-08-13 22:29:49

标签: php wordpress woocommerce orders hook-woocommerce

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

我希望只有在客户没有使用任何优惠券的情况下才能将其显示在处理订单邮件中。

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

感谢。

1 个答案:

答案 0 :(得分: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''processing'状态:\ n \ n

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

(这仅用于测试目的)。

  

原始回答:

是的,可以轻松添加包含2个条件的 if语句

第一个检测优惠券是否未按订单中使用的顺序使用:

empty( $order->get_used_coupons() )

第二个将检测订单是否在&#34;处理&#34;状态:

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

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