【问题标题】:Stop send specific email notification for specific users in Woocommerce停止在 Woocommerce 中为特定用户发送特定电子邮件通知
【发布时间】:2019-07-22 15:52:54
【问题描述】:

我想停止为特定用户/电子邮件发送 Woocommerce 电子邮件。这是我在订单完成时停止发送电子邮件的示例代码。

<?php
add_filter( 'woocommerce_email_headers', 'ieo_ignore_function', 10, 2);

function ieo_ignore_function($headers, $email_id, $order) {
    $list = 'admin@example.com,cs@example.com';
    $user_email = (method_exists( $order, 'get_billing_email' ))? $order->get_billing_email(): $order->billing_email;
    $email_class = wc()->mailer();
    if($email_id == 'customer_completed_order'){
        if(stripos($list, $user_email)!==false){
            remove_action( 'woocommerce_order_status_completed_notification', array( $email_class->emails['WC_Email_Customer_Completed_Order'], 'trigger' ) );
        }
    }
}

但是 WP 继续发送电子邮件。我也尝试在 Woocommerce 文档和源代码(github)和 Stackoverflow 中搜索,但仍然无法解决这个问题。

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

    在此示例中,针对特定客户电子邮件地址禁用“客户完成订单”通知:

    // Disable "Customer completed order" for specifics emails
    add_filter( 'woocommerce_email_recipient_customer_completed_order', 'completed_email_recipient_customization', 10, 2 );
    function completed_email_recipient_customization( $recipient, $order ) {
        // Disable "Customer completed order
        if( is_a('WC_Order', $order) && in_array($order->get_billing_email(), array('jack@mail.com','emma@mail.com') ) ){
            $recipient = '';
        }
        return $recipient;
    }
    

    代码进入您的活动子主题(活动主题)的 function.php 文件中。经过测试并且可以工作。

    注意:过滤器挂钩需要始终返回过滤后的主函数参数


    也可以通过 用户 ID 来完成,例如:

    // Disable "Customer completed order" for specifics User IDs
    add_filter( 'woocommerce_email_recipient_customer_completed_order', 'completed_email_recipient_customization', 10, 2 );
    function completed_email_recipient_customization( $recipient, $order ) {
        // Disable "Customer completed order
        if( is_a('WC_Order', $order) && in_array($order->get_customer_id(), array(25,87) ) ){
            $recipient = '';
        }
        return $recipient;
    }
    

    代码进入您的活动子主题(活动主题)的 function.php 文件中。经过测试并且可以工作。


    类似:Stop specific customer email notification based on payment methods in Woocommerce

    【讨论】:

    • 我看到你经常回答WP问题,你真是WP大师我的朋友。
    • 在文档中哪里可以找到这个参数? woocommerce_email_recipient_customer_completed_order
    • 我已经更新了我的答案...这个钩子是一个复合钩子:woocommerce_email_recipient_{$this-&gt;id}...
    • 太棒了,我以前读过那个课程,但没有注意到这个功能是否可以用于我的目的。
    • @plonknimbuzz 我添加了一个额外的代码,它从用户 ID 而不是电子邮件中执行相同的操作
    【解决方案2】:

    虽然上面的答案有效,但这似乎是更直接的解决方案:

    add_filter( 'woocommerce_email_recipient_customer_completed_order', 'completed_email_recipient_customization', 10, 2 );
    function completed_email_recipient_customization( $recipient, $order ) {
        if( in_array($recipient, ['some@email2block.com', 'another@email2block.com'] ) ) {
            $recipient = '';
        }
        return $recipient;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-31
      • 1970-01-01
      • 2018-01-05
      • 2021-03-09
      • 1970-01-01
      • 2018-07-04
      • 1970-01-01
      • 2014-07-09
      • 2018-09-21
      相关资源
      最近更新 更多