【问题标题】:Send customized new order notification only for pending order status and specific payment methods仅针对待处理订单状态和特定付款方式发送定制的新订单通知
【发布时间】:2020-12-29 14:19:06
【问题描述】:

我正在尝试仅针对“待处理”订单状态而不是“货到付款”付款方式发送有关新订单的通知。但是当客户选择货到付款时,管理员收到了重复的邮件,因为 Woocommerce 将此订单状态从“待处理”更新为“处理中”。

// 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 );
}

【问题讨论】:

  • 这也不起作用: // 仅适用于“待处理”订单状态,而不适用于货到付款方式 if( ! $order->has_status( 'pending' ) && ( get_post_meta($order_id, '_payment_method', true) != 'cod' ) ) return;

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


【解决方案1】:

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

// 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 文件中。经过测试并且可以工作。

自从 WooCommerce 5+: Allow re-sending New Order Notification in WooCommerce 5+

【讨论】:

  • 感谢您的帮助!不幸的是,现在这个功能根本不发送电子邮件。我对其进行了测试,但电子邮件没有以任何付款方式和“待处理”状态到达。
  • 非常感谢!很多国家都需要这个功能,而且效果很好!
猜你喜欢
  • 2023-03-09
  • 2022-10-13
  • 2019-07-26
  • 2020-11-03
  • 2013-02-21
  • 1970-01-01
  • 2019-02-21
  • 2017-12-25
  • 2021-06-13
相关资源
最近更新 更多