php 仅为Woocommerce子订单发送自定义订单状态的电子邮件通知[已关闭]

2ledvvac  于 2023-09-29  发布在  PHP
关注(0)|答案(1)|浏览(109)

已关闭此问题为not reproducible or was caused by typos。它目前不接受回答。

此问题是由打印错误或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这一个是解决的方式不太可能帮助未来的读者。
6天前关闭
Improve this question
我使用下面的代码,添加一个自定义订单状态“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 );
     }
}

我的问题如下:
我使用一个多供应商插件,为每个订单创建自己的子订单号,并将子订单状态与父订单同步。买家收到两封电子邮件。这两个事件发生在订单状态更改时,发送与父订单和子订单相关的电子邮件通知。
要停止发送与父订单相关的电子邮件,并仅将其发送到相关的子订单,请使用以下代码:

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

但不幸的是,这段代码不适用于我的自定义订单状态。
我的代码中有什么地方出错了吗?
如何停止从我的自定义订单状态到相关父订单的电子邮件通知?
任何帮助真的很感激。

yvfmudvl

yvfmudvl1#

您的代码有点过时,有错误和遗漏的东西.当我们发送电子邮件通知时,我们可以直接针对订单状态更改的子订单。
请尝试以下操作:

// 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文件中(或插件中)。测试和作品。
相关信息:

  • 如何获取WooCommerce订单详情
  • 在Woocommerce中添加自定义订单状态“已发货”
  • WooCommerce:添加带有自定义电子邮件通知的自定义订单状态

相关问题