仅针对挂单状态和特定付款方式发送自定义的新订单通知 [英] Send customized new order notification only for pending order status and specific payment methods

查看:72
本文介绍了仅针对挂单状态和特定付款方式发送自定义的新订单通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试仅针对待处理"邮件发送有关新订单的通知.订单状态,而不是货到付款"付款方法.但是当客户选择货到付款时,管理员已经收到重复的邮件,因为Woocommerce从待处理"状态更新了该订单状态.到已处理".

I am trying to send notification about new order only for "pending" order status and not "cash on delivery" payment method. But admin have receive duplicated mails when client choose cash on delivery payment, because Woocommerce update this order status from "pending" to "processed".

// New order notification only for "pending" order status and not "cash on delivery" payment method

add_action( 'woocommerce_checkout_order_processed', 'pending_new_order_notification', 10, 1 );
function pending_new_order_notification( $order_id ) {
    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );
    $payment_title = $order->get_payment_method_title();

    // Only for "pending" order status and not Cash on delivery payment method 
    if( ! $order->has_status( 'pending' )  && ( $payment_title != 'cod' ) ) return;

    // Get an instance of the WC_Email_New_Order object
    $wc_email = WC()->mailer()->get_emails()['WC_Email_New_Order'];

    ## -- Customizing Heading, subject (and optionally add recipients)  -- ##
    // Change Subject
    $wc_email->settings['subject'] = __('{site_title} - Новый заказ ({order_number}) - {order_date} ожидает оплату');
    // Change Heading
    $wc_email->settings['heading'] = __('Новый заказ'); 
    $wc_email->settings['recipient'] .= ',name@email.com'; // Add email recipients (coma separated)

    // Send "New Email" notification (to admin)
     $wc_email->trigger( $order_id );
}

推荐答案

您的代码中存在一些错误和遗漏,请尝试以下操作:

There are some mistakes and missing things in your code, so try the following instead:

// Send email
add_action( 'woocommerce_checkout_order_processed', 'pending_custom_new_order_notification', 20, 3 );
function pending_custom_new_order_notification( $order_id, $posted_data, $order ) {
    // Only for "pending" order status and not Cash on delivery payment method 
    if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
        // Send "New Order" email
        $wc_email = WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger( $order_id );
    }
}

// Custom email subject
add_filter( 'woocommerce_email_subject_new_order', 'custom_new_order_email_subject', 20, 2 );
function custom_new_order_email_subject( $formated_subject, $order ){
    // Only for "pending" order status and not Cash on delivery payment method 
    if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
        $formated_subject = sprintf( __('%s - Новый заказ (%s) - %s ожидает оплату', 'woocommerce'), 
            get_bloginfo( 'name' ),
            $order->get_order_number(), // Order ID (or the order number)
            $order->get_date_modified()->date_i18n('F j, Y') // Formatted date modified
        );
    }
    return $formated_subject;
}

// Custom email heading
add_filter( 'woocommerce_email_heading_new_order', 'custom_new_order_email_heading', 20, 2 );
function custom_new_order_email_heading( $heading_txt, $order ){
    // Only for "pending" order status and not Cash on delivery payment method 
    if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
        $heading_txt = __('Новый заказ', 'woocommerce');
    }
    return $heading_txt;
}

// Custom email recipient
add_filter( 'woocommerce_email_recipient_new_order', 'custom_new_order_email_recipient', 20, 2 );
function custom_new_order_email_recipient( $recipient, $order ){
    // Only for "pending" order status and not Cash on delivery payment method 
    if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
        $recipient .= ',name@email.com';
    }
    return $recipient;
}

代码进入活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

自WooCommerce 5+起: 查看全文

相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆