简体   繁体   English

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

[英]Add custom order status and send email on status change in Woocommerce

I want to add custom order status in my woocommerce site.And whenever status changes to that custom status I want to send mail.我想在我的 woocommerce 网站中添加自定义订单状态。每当状态更改为该自定义状态时,我都想发送邮件。
I have tried Send an email notification when custom order status changes in WooCommerce我尝试在 WooCommerce 中自定义订单状态更改时发送电子邮件通知
as well as https://github.com/sarun007/custom-email-plugin/tree/master以及https://github.com/sarun007/custom-email-plugin/tree/master
But didn't worked但没有奏效
I am using woocommerce 3.2.6 version我正在使用 woocommerce 3.2.6版本

New version code : Add a new order status that Send an email notification in WooCommerce 4+新版本代码在 WooCommerce 4+ 中添加发送电子邮件通知的新订单状态


To make it work for a new custom order status (here "Awaiting delivery") you should need:要使其适用于新的自定义订单状态(此处为“等待交货”),您应该需要:

  • to register the custom new order status first,首先注册自定义新订单状态,
  • to display it in bulk edit dropdown on admin orders list (optional) ,在管理订单列表(可选)的批量编辑下拉菜单中显示它,
  • to display it in order edit pages status dropdown按顺序显示它编辑页面状态下拉列表
  • to send an customized email notification when an order get this custom status.当订单获得此自定义状态时发送自定义电子邮件通知。

The code:代码:

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

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。

Tested and works (should work on any Woocommerce versions above 2.5)测试和工作(应该适用于 2.5 以上的任何 Woocommerce 版本)

In others function if you use the WC_Order method update_status() to change an order to 'awaiting-delivery' status like:在其他函数中,如果您使用 WC_Order 方法update_status()将订单更改为“等待交付”状态,例如:

 $order->update_status();

the related email notification will be sent too.相关的电子邮件通知也将发送

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce - 发送自定义订单状态更改的自定义电子邮件 - WooCommerce - send custom email on custom order status change 在 WooCommerce 中自定义订单状态更改时发送“保留”email - Send "on hold" email on custom order status change in WooCommerce 在 Woocommerce 中触发订单自定义状态更改的电子邮件 - Trigger email on order custom status change in Woocommerce WooCommerce订单状态处理然后将电子邮件与客户一起发送到自定义电子邮件 - WooCommerce order status processing then send an email to custom email along with customer 在 WooCommerce 4+ 中添加发送电子邮件通知的新订单状态 - Add a new order status that Send an email notification in WooCommerce 4+ 如何在WooCommerce中使用自定义订单状态更改订单状态 - How to change the order status with a custom order status in WooCommerce 在自定义订单状态更改时更改 Woocommerce 预订状态 - Change Woocommerce booking status on Custom Order Status change Woocommerce自定义订单状态更改日期 - Woocommerce custom order status change date 通过自定义状态向管理员发送新订单电子邮件 - Send New Order email to Admin on custom Status 需要具有自定义 Woocommerce 订单状态的批量状态更改功能 - Need Bulk Status Change function with a Custom Woocommerce Order Status
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM