当 WooCommerce 中的自定义订单状态发生变化时发送电子邮件通知 [英] Send an email notification when custom order status changes in WooCommerce

查看:63
本文介绍了当 WooCommerce 中的自定义订单状态发生变化时发送电子邮件通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WooCommerce 中创建了一个名为 Back Order (wc-backorder) 的自定义订单状态:

I have created a custom order status in my WooCommerce called Back Order (wc-backorder):

/**
     * Add custom status to order list
*/
    add_action( 'init', 'register_custom_post_status', 10 );
    function register_custom_post_status() {
        register_post_status( 'wc-backorder', array(
            'label'                     => _x( 'Back Order', '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( 'Back Order <span class="count">(%s)</span>', 'Back Order <span class="count">(%s)</span>', 'woocommerce' )
        ) );

}

/**
 * Add custom status to order page drop down
 */
add_filter( 'wc_order_statuses', 'custom_wc_order_statuses' );
function custom_wc_order_statuses( $order_statuses ) {
    $order_statuses['wc-backorder'] = _x( 'Back Order', 'Order status', 'woocommerce' );
    return $order_statuses;
}

现在我想在收到已提供状态报价的订单时收到一封电子邮件.我根据这篇有用的文章创建了一个插件:如何添加自定义 WooCommerce 电子邮件

Now I want to receive an email whenever an order is received that has been given the status quote. I've created a plugin based on this helpful article: How to Add a Custom WooCommerce Email

此链接包含我的插件源代码和functions.php代码.

This link is containing my plugin source code and the functions.php code.

我在 function.php 中添加了钩子:

I added hook in function.php:

add_action( 'woocommerce_order_status_wc-order-confirmed', array( WC(), 'send_transactional_email' ), 10, 10 );

function so_27112461_woocommerce_email_actions( $actions ){
    $actions[] = 'woocommerce_order_status_wc-order-confirmed';
    return $actions;
}
add_filter( 'woocommerce_email_actions', 'so_27112461_woocommerce_email_actions' );

当订单更改为延期交货"状态时,没有任何反应.

Nothing happens when an order changed into the 'Back Order' status.

有什么想法吗?

我尝试了大量不同的钩子,但似乎无法运行触发函数.

I've tried loads of different hooks but I can't seem to get the trigger function to run.

我使用的是最新版本的 WordPress 和 WooCommerce (3.0+)

I am on latest versions of WordPress and WooCommerce (3.0+)

谢谢

推荐答案

- EDIT/UPDATE -

由于您使用的代码教程对于这个新的大型主要版本 3.0+ 来说真的已经过时了(2013 年),这个挂在 woocommerce_order_status_changed 动作钩子中的自定义函数将执行工作.因此,当订单状态更改为您的自定义状态时,您将能够发送自定义处理电子邮件通知.

As the code tutorial you are using is really outdated (2013) for this new mega major version 3.0+, this custom function hooked in woocommerce_order_status_changed action hook will do the job. So You will be able to send a customized Processing email notification, when order status is changed to your custom status.

这是 WC 3.0+ 的工作和测试代码:

Here is that working and tested code for WC 3.0+:

add_action('woocommerce_order_status_changed', 'backorder_status_custom_notification', 10, 4);
function backorder_status_custom_notification( $order_id, $from_status, $to_status, $order ) {

   if( $order->has_status( 'backorder' )) {

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

        // Customizing Heading and subject In the WC_email processing Order object
        $email_notifications['WC_Email_Customer_Processing_Order']->heading = __('Your processing Back order','woocommerce');
        $email_notifications['WC_Email_Customer_Processing_Order']->subject = 'Your {site_title} processing Back order receipt from {order_date}';

        // Sending the customized email
        $email_notifications['WC_Email_Customer_Processing_Order']->trigger( $order_id );
    }

}

此代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

因为你的自定义状态是wc-backorder,而不是wc-order-confirmed,你只需要到处替换wc-order-confirmed by wc-backorder.

AS your custom status is wc-backorder, but not wc-order-confirmed, you just need to replace everywhere wc-order-confirmed by wc-backorder.

要使其工作,您必须以这种方式更改最后两个挂钩的函数:

To make it work, you will have to change the 2 last hooked functions this way:

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


add_filter( 'woocommerce_email_actions', 'filter_woocommerce_email_actions' );
function filter_woocommerce_email_actions( $actions ){
    $actions[] = 'woocommerce_order_status_wc-backorder';
    return $actions;
}

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

这应该有效(我无法测试它,因为没有自定义插件的代码).

This should work (I can't test it as there is no the code of your custom plugin).

参考源代码:woocommerce_order_status_{$this->status_transition[to]} 动作钩子

这篇关于当 WooCommerce 中的自定义订单状态发生变化时发送电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆