禁用WooCommerce订单电子邮件通知对于具有自定义状态的订单。

4

我已在网上搜索并查阅了WooCommerce文档,以解决禁用顾客在WooCoomerce下单时发送的“确认邮件”的问题。

我还想禁用发给管理员的“新订单”邮件。

但仅当订单具有自定义状态“mystatus”时才这么做,这取决于客户订购的商品。

我试过像这样添加,但没有成功:

remove_action( 'woocommerce_order_status_mystatus_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger' ) );?>

有什么建议吗?


以下是如何更改特定订单状态的方法:

add_action( 'woocommerce_thankyou','woocommerce_thankyou_change_order_status', 10, 1 );
function woocommerce_thankyou_change_order_status( $order_id ){
if( ! $order_id ) return;

$order = wc_get_order( $order_id );
$user_id = $order->get_user_id();

if( ($order->get_status() == 'processing'  || $order->get_status() == 'pending' || $order->get_status() == 'on-hold' ) && dokan_is_user_seller( $user_id ) && is_user_logged_in()) {
    $order->update_status( 'mystatus' );
  }
}

默认的WooCommerce电子邮件可以在WooCommerce >>设置>>电子邮件中打开/关闭或配置。之后,您可以在SO上找到如何手动发送WooCommerce电子邮件的方法。我认为这是最好的方法。 - Mr. Jo
谢谢,但我想保持默认的电子邮件不变,只有当订单具有我的自定义订单状态时,我才想禁用它(并且在“设置->电子邮件”中看不到任何选项)。 - BTB
但是这种方式比另一种方式简单得多... 只需在订单具有特定状态时禁用要发送的电子邮件,并以编程方式发送邮件。 这真的不是什么大问题... - Mr. Jo
2个回答

7

不需要对WooCommerce设置进行任何更改。

除非您明确提供,否则不会发送关于自定义订单状态的电子邮件通知。

但是,默认的电子邮件通知会发送,因为woocommerce_thankyou钩子在发送电子邮件通知后执行

因此,请使用woocommerce_checkout_order_created钩子(在发送电子邮件通知之前执行)相对于woocommerce_thankyou来更改订单状态,则无论如何都不会发送任何电子邮件。

function action_woocommerce_checkout_order_created( $order ) {
    // Get user ID
    $user_id = $order->get_user_id();
    
    // Compare
    if ( ( $order->get_status() == 'processing'  || $order->get_status() == 'pending' || $order->get_status() == 'on-hold' ) && dokan_is_user_seller( $user_id ) && is_user_logged_in() ) {
        // Update status
        $order->update_status( 'mystatus' );
    }
}
add_action( 'woocommerce_checkout_order_created', 'action_woocommerce_checkout_order_created', 10, 1 );

注意:如果你想在任何其他情况下禁用电子邮件通知,你可以使用woocommerce_email_recipient_{$email_id}筛选器组合钩子,设置正确的电子邮件ID,有一种选项可以禁用电子邮件通知。

例如:

// Admin - new order email notification
// Customer - on hold
// Customer - processing
// Customer - pending
function filter_woocommerce_email_recipient( $recipient, $order, $email ) { 
    if ( ! $order || ! is_a( $order, 'WC_Order' ) ) return $recipient;
    
    // Has order status
    if ( $order->has_status( 'your-order-status' ) ) {
        $recipient = '';
    }

    return $recipient;
}
add_filter( 'woocommerce_email_recipient_new_order', 'filter_woocommerce_email_recipient', 10, 3 );
add_filter( 'woocommerce_email_recipient_customer_on_hold_order', 'filter_woocommerce_email_recipient', 10, 3 );
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'filter_woocommerce_email_recipient', 10, 3 );
add_filter( 'woocommerce_email_recipient_customer_pending_order', 'filter_woocommerce_email_recipient', 10, 3 );

非常感谢您提供如此详细的答案。这非常有道理(当然也是有效的)。 - BTB

3

WooCommerce >> 设置 >> 电子邮件 下禁用您想仅在订单具有自定义状态的情况下发送的邮件。

现在只有在订单具有正确状态的情况下才发送邮件:

add_action( 'woocommerce_thankyou', 'woocommerce_thankyou_change_order_status', 10, 1 );
function woocommerce_thankyou_change_order_status( $order_id ) {
    if ( ! $order_id ) {
        return;
    }

    $order   = wc_get_order( $order_id );
    $user_id = $order->get_user_id();

    if ( ( $order->get_status() == 'processing' || $order->get_status() == 'pending' || $order->get_status() == 'on-hold' ) && dokan_is_user_seller( $user_id ) && is_user_logged_in() ) {
        $order->update_status( 'mystatus' );

        $email_oc = new WC_Email_Customer_Completed_Order();
        $email_oc->trigger($order_id);
    }
}

您可以使用PHP发送任何WooCommerce邮件。


啊,现在我明白你之前的意思了。看起来是个好主意!我尝试了一下,只是替换了部分内容,以便发送正确的电子邮件:WC_Email_Customer_Processing_Order。但是这会导致错误 Uncaught Error: Class 'WC_Email_Customer_Processing_Order' not found in ... 有什么建议吗? - BTB
也许你需要导入这个类。你的 IDE (集成开发环境)提示了什么呢?通常情况下,IDE 会告诉你是否需要导入一个类。 - Mr. Jo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接