我在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;
}
现在,我想在收到状态报价的任何订单时收到一封电子邮件。我根据此有用的文章创建了一个插件:如何添加自定义WooCommerce电子邮件
该链接包含我的插件源代码和functions.php代码。
我在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' );
当订单更改为“延期交货”状态时,什么也没有发生。
有任何想法吗?
我已经尝试了不同钩子的加载,但是似乎无法使触发函数运行。
我正在使用最新版本的WordPress和WooCommerce(3.0+)
谢谢
-编辑/更新-
由于您所使用的代码教程对于此新的巨型主要版本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]}
动作钩子
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句