【问题标题】:Remove woocommerce complete or processing email based on order meta根据订单元数据删除 woocommerce 完成或处理电子邮件
【发布时间】:2024-01-01 01:52:02
【问题描述】:

我正在尝试删除基于某些订单元数据的处理(或完成)电子邮件。

我正在使用 POS 系统并让客户通过客户发票电子邮件付款 - 初始订单状态为待付款。我想 a) 测试订单是否使用 pos,b) 删除“处理”或“完成”电子邮件。

我似乎无法让 if 语句逻辑工作。我很确定元键是“_pos”,值是“1”或“0”。

Here's my myphp screem shot of wp_postmeta

add_action( 'woocommerce_email', 'removing_POS_emails' );
function removing_POS_emails( $email_class, $order_id ) {

     //Remove the Processing email for POS emails
     $pos_test = get_post_meta( $order_id, '_pos', true );
     if ( $pos_test == "1" ) {
         remove_action( 'woocommerce_order_status_pending_to_processing_notification', array( $email_class->emails['WC_Email_Customer_Processing_Order'], 'trigger' ) );
     }
}

我错过了什么吗?可以在 woocommerce_email 挂钩中使用 post meta 吗?

如果我得到正确的 if 语句,我相信我可以删除处理/完成电子邮件,甚至更改电子邮件类并创建自定义处理电子邮件。

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce email-notifications


    【解决方案1】:

    更新(与$order参数相关的另一个钩子上有一个错误)

    这是正确的做法:

    add_filter( 'woocommerce_email_recipient_customer_processing_order', 'conditional_email_notification', 10, 2 );
    add_filter( 'woocommerce_email_recipient_customer_completed_order', 'conditional_email_notification', 10, 2 );
    function conditional_email_notification( $recipient, $order ) {
        if( is_admin() ) return $recipient;
    
        if ( get_post_meta( $order->get_id(), '_pos', true ) ){
            return '';
        }
        return $recipient;
    }
    

    此代码位于您的活动子主题(或主题)的 function.php 文件中。经过测试并且有效。


    类似答案:Avoid customer email notification for a specific product category in Woocommerce

    【讨论】:

    • 太棒了!谢谢@LoicTheAztec。正是我需要的答案
    • 很抱歉重新唤醒野兽,但我注意到此代码会影响我的 Woocommerce->settings->emails 页面。我只为“已完成”电子邮件激活了此代码,但设置页面上“已完成”电子邮件选项下方的所有内容都消失了。 [imgur.com/a/5vMPt] - 添加了过滤器; [imgur.com/a/81ZgN] - 没有过滤器。您在测试中遇到过这种情况吗?