【问题标题】:Add coupon to the processing order email only if the customer have not used one仅当客户未使用优惠券时,才将优惠券添加到处理订单电子邮件中
【发布时间】:2016-08-13 22:29:48
【问题描述】:

我遇到了这个在订单邮件中添加优惠券的 sn-p。

我想让它只在客户没有使用任何优惠券的情况下出现在处理订单邮件中。

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

谢谢。

【问题讨论】:

    标签: php wordpress woocommerce orders hook-woocommerce


    【解决方案1】:

    @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'状态使用这个:

    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() )
    

    第二个会检测订单是否处于“处理”状态:

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

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

    这是您自定义的代码 sn-p:

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

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

    【讨论】:

    • 但是,我使用优惠券对其进行了测试,并收到了一封电子邮件订单,其中包含以下文本“致命错误:调用 C:\xampp\...中字符串上的成员函数 get_used_coupons()。 \Divi\functions.php 第 8527 行"
    • 对不起,现在没有错误,但电子邮件订单中也没有优惠券。我尝试了尽可能多的方法......
    • 我发布了另一个关于此问题的帖子,询问如何仅在已完成的订单邮件中显示该优惠券。 link
    猜你喜欢
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 2021-02-18
    • 2019-07-21
    • 1970-01-01
    相关资源
    最近更新 更多