仅针对 Woocommerce 子订单发送有关自定义订单状态的电子邮件通知

问题描述 投票:0回答:1

我使用下面的代码来添加自定义订单状态“readytocollect”和“shipped”。当订单状态发生变化时,会向买家发送一封电子邮件,通知他的订单正在组装或交付(有效):

// Enable custom statuses for WooCommerce Orders
add_action('init', 'register_custom_order_statuses');
function register_custom_order_statuses() {
    register_post_status('wc-shipped', array(
        'label' => __( 'shipped', 'woocommerce' ),
        'public' => true,
        'exclude_from_search' => false,
        'show_in_admin_all_list' => true,
        'show_in_admin_status_list' => true,
        'label_count' => _n_noop('shipped <span class="count">(%s)</span>', 'shipped <span class="count">(%s)</span>')
    ));
    register_post_status('wc-readytocollect', array(
        'label' => __( 'readytocollect', 'woocommerce' ),
        'public' => true,
        'exclude_from_search' => false,
        'show_in_admin_all_list' => true,
        'show_in_admin_status_list' => true,
        'label_count' => _n_noop('readytocollect <span class="count">(%s)</span>', 'readytocollect <span class="count">(%s)</span>')
    ));
}

add_action('woocommerce_order_status_changed', 'my_notification_shipped');
function my_notification_shipped ($order_id) {
   global $woocommerce;
   $order = new WC_Order( $order_id );
   if($order->status === 'shipped' ) {
      //HERE IS THE ISSUE
       $order->get_order_number();
      // Create a mailer
      $mailer = $woocommerce->mailer();
      $message_body =     "
                <html>
                    <head>
                    </head>
                    <body>
                        <h1>shipped</h1>
                        <h1></h1>
                    </body>
                    
                    </html>
                ";
      $message = $mailer->wrap_message(
      // Message head and message body.
      sprintf( __( 'order %s shipped' ), $order->get_order_number() ), $message_body );
      // Send email
      $mailer->send( $order->billing_email, sprintf( __( 'order %s shipped' ), $order->get_order_number() ), $message );
     }
}

add_action('woocommerce_order_status_changed', 'my_notification_readytocollect');
function my_notification_readytocollect ($order_id) {
   global $woocommerce;
   $order = new WC_Order( $order_id );
   if($order->status === 'readytocollect' ) {
      //HERE IS THE ISSUE
        $order->get_order_number();
      // Create a mailer
      $mailer = $woocommerce->mailer();
      $message_body =                 "
                <html>
                    <head>

 </head>
                    <body>

                        <h1>readytocollect</h1>
                        <h2></h2>
                    </body>
                    </html>
                ";
      $message = $mailer->wrap_message(
      // Message head and message body.
      sprintf( __( 'order %s readytocollect' ), $order->get_order_number() ), $message_body );
      // Send email
      $mailer->send( $order->billing_email, sprintf( __( 'order %s readytocollect' ), $order->get_order_number() ), $message );
     }
}

我的问题如下:

我使用多供应商插件,为每个订单创建自己的子订单编号,并将子订单状态与父订单同步。买家收到 2 封电子邮件。当订单状态发生变化时,会发生这 2 个事件,发送与父订单和子订单相关的电子邮件通知。

要停止发送与父订单相关的电子邮件,并仅将其发送到相关子订单,请使用以下代码:

add_filter( 'woocommerce_email_recipient_customer_readytocollect_order', 'disable_email_for_parent_order', 10,3 );
add_filter( 'woocommerce_email_recipient_customer_shipped_order', 'disable_email_for_parent_order', 10,3 );
function disable_email_for_parent_order( $recipient, $order, $object ){
    if( wp_get_post_parent_id( $order->get_id() ) ){
        return $recipient;
    } else{
        return;
    }
}

但不幸的是,此代码不适用于我的自定义订单状态。

我的代码有什么地方有错误吗?

如何停止从我的自定义订单状态向相关父订单发送电子邮件通知?

非常感谢任何帮助。

php woocommerce orders status email-notifications
1个回答
0
投票

您的代码有点过时,有错误和遗漏…当我们发送电子邮件通知时,我们可以直接针对订单状态变化来定位子订单。

尝试以下操作:

// Enable custom statuses for WooCommerce Orders
add_action('init', 'register_custom_order_statuses');
function register_custom_order_statuses() {
    register_post_status('wc-shipped', array(
        'label' => __( 'Shipped', 'woocommerce' ),
        'public' => true,
        'exclude_from_search' => false,
        'show_in_admin_all_list' => true,
        'show_in_admin_status_list' => true,
        'label_count' => _n_noop('Shipped <span class="count">(%s)</span>', 'Shipped <span class="count">(%s)</span>')
    ));
    register_post_status('wc-readytocollect', array(
        'label' => __( 'Ready to collect', 'woocommerce' ),
        'public' => true,
        'exclude_from_search' => false,
        'show_in_admin_all_list' => true,
        'show_in_admin_status_list' => true,
        'label_count' => _n_noop('Ready to collect <span class="count">(%s)</span>', 'Ready to collect <span class="count">(%s)</span>')
    ));
}

// Add a custom order status to list of WC Order statuses
add_filter('wc_order_statuses', 'add_custom_order_statuses');
function add_custom_order_statuses($order_statuses) {
    $new_order_statuses = array();

    // add new order status before processing
    foreach ($order_statuses as $key => $status) {
        $new_order_statuses[$key] = $status;
        if ('wc-processing' === $key) {
            $new_order_statuses['wc-shipped']        = __('Shipped', 'woocommerce' );
            $new_order_statuses['wc-readytocollect'] = __('Ready to collect', 'woocommerce' );
        }
    }
    return $new_order_statuses;
}

// Send custom email notifications to sub orders only
add_action( 'woocommerce_order_status_changed', 'custom_order_statuses_email_notifications', 10, 4 );
function custom_order_statuses_email_notifications ($order_id, $from_status, $to_status, $order) {
    // Targeting sub-orders only
    if ( ! $order->get_parent_id() ) return;

    if ( $to_status === 'shipped' ) {
        $status_label  = __( 'shipped', 'woocommerce' );
    } 
    elseif ( $to_status === 'readytocollect' ) {
        $status_label  = __( 'Ready to collect', 'woocommerce' );
    }

    if ( isset($status_label) ) {
        $mailer        = WC()->mailer(); // load the mailer class.
        $email_subject = sprintf( __( 'Order %s %s' ), $order->get_order_number(), $status_label );
        $message_body  = '<html>
        <head></head>
        <body>
            <h1>'.$status_label.'</h1>
            <p></p>
        </body>
        </html>';
        $message = $mailer->wrap_message( $email_subject, $message_body );
        $mailer->send( $order->get_billing_email(), $email_subject, $message ); // Send email
    }
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

相关:

© www.soinside.com 2019 - 2024. All rights reserved.