Php 在Woocommerce中添加自定义订单状态并发送状态更改电子邮件

Php 在Woocommerce中添加自定义订单状态并发送状态更改电子邮件,php,wordpress,woocommerce,status,orders,Php,Wordpress,Woocommerce,Status,Orders,我想在我的woocommerce网站中添加自定义订单状态。当状态更改为该自定义状态时,我想发送邮件。 我尝试过以及但没有成功我正在使用woocommerce 3.2.6版本新版本代码: 要使其适用于新的自定义订单状态(此处为“等待交货”),您需要: 要先注册自定义新订单状态 要在管理订单列表上的批量编辑下拉列表中显示它(可选) 按顺序显示“编辑页面状态”下拉列表 在订单获得此自定义状态时发送自定义电子邮件通知 守则: // register a custom post status 'aw

我想在我的woocommerce网站中添加自定义订单状态。当状态更改为该自定义状态时,我想发送邮件。

我尝试过
以及
但没有成功
我正在使用woocommerce 3.2.6版本

新版本代码


要使其适用于新的自定义订单状态(此处为“等待交货”),您需要:

  • 要先注册自定义新订单状态
  • 要在管理订单列表上的批量编辑下拉列表中显示它(可选)
  • 按顺序显示“编辑页面状态”下拉列表
  • 在订单获得此自定义状态时发送自定义电子邮件通知
守则:

// register a custom post status 'awaiting-delivery' for Orders
add_action( 'init', 'register_custom_post_status', 20 );
function register_custom_post_status() {
    register_post_status( 'wc-awaiting-delivery', array(
        'label'                     => _x( 'Awaiting delivery', 'Order status', 'woocommerce' ),
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Awaiting delivery <span class="count">(%s)</span>', 'Awaiting delivery <span class="count">(%s)</span>', 'woocommerce' )
    ) );
}

// Adding custom status 'awaiting-delivery' to order edit pages dropdown
add_filter( 'wc_order_statuses', 'custom_wc_order_statuses', 20, 1 );
function custom_wc_order_statuses( $order_statuses ) {
    $order_statuses['wc-awaiting-delivery'] = _x( 'Awaiting delivery', 'Order status', 'woocommerce' );
    return $order_statuses;
}

// Adding custom status 'awaiting-delivery' to admin order list bulk dropdown
add_filter( 'bulk_actions-edit-shop_order', 'custom_dropdown_bulk_actions_shop_order', 20, 1 );
function custom_dropdown_bulk_actions_shop_order( $actions ) {
    $actions['mark_awaiting-delivery'] = __( 'Mark Awaiting delivery', 'woocommerce' );
    return $actions;
}

// Adding action for 'awaiting-delivery'
add_filter( 'woocommerce_email_actions', 'custom_email_actions', 20, 1 );
function custom_email_actions( $action ) {
    $actions[] = 'woocommerce_order_status_wc-awaiting-delivery';
    return $actions;
}

add_action( 'woocommerce_order_status_wc-awaiting-delivery', array( WC(), 'send_transactional_email' ), 10, 1 );

// Sending an email notification when order get 'awaiting-delivery' status
add_action('woocommerce_order_status_awaiting-delivery', 'backorder_status_custom_notification', 20, 2);
function backorder_status_custom_notification( $order_id, $order ) {
    // HERE below your settings
    $heading   = __('Your Awaiting delivery order','woocommerce');
    $subject   = '[{site_title}] Awaiting delivery order ({order_number}) - {order_date}';

    // Getting all WC_emails objects
    $mailer = WC()->mailer()->get_emails();

    // Customizing Heading and subject In the WC_email processing Order object
    $mailer['WC_Email_Customer_Processing_Order']->heading = $heading;
    $mailer['WC_Email_Customer_Processing_Order']->subject = $subject;

    // Sending the customized email
    $mailer['WC_Email_Customer_Processing_Order']->trigger( $order_id );
}
相关的电子邮件通知也将被发送


谢谢Loic,我会试试这个我已经试过了,但它不起作用。它在下拉列表中添加了状态,但在电子邮件中没有设置settings@ShrutiKharge这里的答案只是回答你最初的问题。在后端为自定义订单状态添加新的电子邮件通知,这只是另一个与您在此处提出的问题无关的问题。这非常有效,效果非常好。然而,我想知道是否有可能更改电子邮件的正文?就像代码片段使用$heading和$subject一样,是否可以使用$message\u body或类似的东西?@TobiasM我不这么认为……在这种情况下,您应该需要一些不同的、更复杂的自定义电子邮件通知。
$order->update_status();