当 WooCommerce 中的自定义订单状态发生变化时发送电子邮件通知
Send an email notification when custom order status changes in WooCommerce
我在我的 WooCommerce 中创建了一个名为延期交货的自定义订单状态(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;
}
现在我想在收到状态报价的订单时收到一封电子邮件。我根据这篇有用的文章创建了一个插件:How to Add a Custom WooCommerce Email
This link 包含我的插件源代码和 functions.php 代码。
我在function.php中添加了hook:
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' );
当订单变为 'Back Order' 状态时没有任何反应。
有什么想法吗?
我尝试了很多不同的钩子,但我似乎无法获得 运行 的触发功能。
I am on latest versions of WordPress and WooCommerce (3.0+)
谢谢
add_action("woocommerce_order_status_changed", "my_custom_notification");
function my_custom_notification($order_id, $checkout=null) {
global $woocommerce;
$order = new WC_Order( $order_id );
if($order->status === 'backorder' ) {
// Create a mailer
$mailer = $woocommerce->mailer();
$message_body = __( 'Hello world!!!' );
$message = $mailer->wrap_message(
// Message head and message body.
sprintf( __( 'Order %s received' ), $order->get_order_number() ), $message_body );
// Cliente email, email subject and message.
$mailer->send( $order->billing_email, sprintf( __( 'Order %s received' ), $order->get_order_number() ), $message );
}
}
试试这个
- 编辑/更新 -
由于您使用的代码教程对于这个新的大型主要版本 3.0+ 来说确实已经过时 (2013),因此挂钩在 woocommerce_order_status_changed
动作挂钩中的这个自定义函数将做这份工作。因此,当订单状态更改为您的自定义状态时,您将能够发送自定义处理电子邮件通知。
这是 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
来自 wc-backorder
.
要使其正常工作,您必须这样更改最后两个挂钩函数:
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 文件或任何插件文件。
这应该可以工作(我无法测试它,因为没有您自定义插件的代码).
参考源码:woocommerce_order_status_{$this->status_transition[to]}
action hook
我在我的 WooCommerce 中创建了一个名为延期交货的自定义订单状态(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;
}
现在我想在收到状态报价的订单时收到一封电子邮件。我根据这篇有用的文章创建了一个插件:How to Add a Custom WooCommerce Email
This link 包含我的插件源代码和 functions.php 代码。
我在function.php中添加了hook:
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' );
当订单变为 'Back Order' 状态时没有任何反应。
有什么想法吗?
我尝试了很多不同的钩子,但我似乎无法获得 运行 的触发功能。
I am on latest versions of WordPress and WooCommerce (3.0+)
谢谢
add_action("woocommerce_order_status_changed", "my_custom_notification");
function my_custom_notification($order_id, $checkout=null) {
global $woocommerce;
$order = new WC_Order( $order_id );
if($order->status === 'backorder' ) {
// Create a mailer
$mailer = $woocommerce->mailer();
$message_body = __( 'Hello world!!!' );
$message = $mailer->wrap_message(
// Message head and message body.
sprintf( __( 'Order %s received' ), $order->get_order_number() ), $message_body );
// Cliente email, email subject and message.
$mailer->send( $order->billing_email, sprintf( __( 'Order %s received' ), $order->get_order_number() ), $message );
}
}
试试这个
- 编辑/更新 -
由于您使用的代码教程对于这个新的大型主要版本 3.0+ 来说确实已经过时 (2013),因此挂钩在 woocommerce_order_status_changed
动作挂钩中的这个自定义函数将做这份工作。因此,当订单状态更改为您的自定义状态时,您将能够发送自定义处理电子邮件通知。
这是 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
来自 wc-backorder
.
要使其正常工作,您必须这样更改最后两个挂钩函数:
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 文件或任何插件文件。
这应该可以工作(我无法测试它,因为没有您自定义插件的代码).
参考源码:woocommerce_order_status_{$this->status_transition[to]}
action hook