php - WooCommerce 4 中自定义订单状态的自定义标题和主题电子邮件通知
问题描述
我已经创建了自定义状态发货但不幸的是,当我们将订单状态更改为发货而不是我们在功能发货状态自定义通知中定义的状态时,邮件发送默认处理,你能帮帮我吗
/**
* Add custom status to order list
*/
add_action( 'init', 'register_custom_post_status', 10 );
function register_custom_post_status() {
register_post_status( 'wc-shipped', array(
'label' => _x( 'Shipped', '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( 'Shipped <span class="count">(%s)</span>', 'Shipped <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-shipped'] = _x( 'Shipped', 'Order status', 'woocommerce' );
return $order_statuses;
}
add_action('woocommerce_order_status_changed', 'shipped_status_custom_notification', 10, 4);
function shipped_status_custom_notification( $order_id, $from_status, $to_status, $order ) {
if( $order->has_status( 'shipped' )) {
// 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 Order shipped','woocommerce');
$email_notifications['WC_Email_Customer_Processing_Order']->subject = 'Your {site_title} order shipped receipt from {order_date}';
// Sending the customized email
$email_notifications['WC_Email_Customer_Processing_Order']->trigger( $order_id );
}
}
add_action( 'woocommerce_order_status_wc-shipped', 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-shipped';
return $actions;
}
解决方案
自 2018 年以来,WooCommerce 通过所有插件更新发生了很大变化,因此要获取带有自定义标题和主题的自定义订单状态的电子邮件通知,请改用以下内容:
// Register (add) a new post status
add_action( 'init', 'register_custom_post_status', 10 );
function register_custom_post_status() {
register_post_status( 'wc-shipped', array(
'label' => _x( 'Shipped', '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( 'Shipped <span class="count">(%s)</span>', 'Shipped <span class="count">(%s)</span>', 'woocommerce' )
) );
}
// Enable this custom status status for WooCommerce order (and sort them)
add_filter( 'wc_order_statuses', 'custom_wc_order_statuses' );
function custom_wc_order_statuses( $order_statuses ) {
$sorted_order_statuses = [];
foreach( $order_statuses as $key => $label ) {
$sorted_order_statuses[$key] = $order_statuses[$key];
if( 'wc-processing' === $key ){
$sorted_order_statuses['wc-shipped'] = _x( 'Shipped', 'Order status', 'woocommerce' );
}
}
return $sorted_order_statuses;
}
// Adding this custom status 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_shipped'] = __( 'Mark Waiting in tree', 'woocommerce' );
return $actions;
}
// Enable the action
add_filter( 'woocommerce_email_actions', 'filter_woocommerce_email_actions' );
function filter_woocommerce_email_actions( $actions ){
$actions[] = 'woocommerce_order_status_wc-shipped';
return $actions;
}
// Send Customer Processing Order email notification when order status get changed from "tree" to "processing"
add_action('woocommerce_order_status_changed', 'shipped_status_custom_notification', 10, 4);
function shipped_status_custom_notification( $order_id, $from_status, $to_status, $order ) {
if( 'shipped' === $to_status ) {
// The email notification type
$email_key = 'WC_Email_Customer_Processing_Order';
// Get specific WC_emails object
$email_obj = WC()->mailer()->get_emails()[$email_key];
// Sending the customized email
$email_obj->trigger( $order_id );
}
}
// Customize email heading for this custom status email notification
add_filter( 'woocommerce_email_heading_customer_processing_order', 'email_heading_customer_shipped_order', 10, 2 );
function email_heading_customer_shipped_order( $heading, $order ){
if( $order->has_status( 'shipped' ) ) {
$email_key = 'WC_Email_Customer_Processing_Order'; // The email notification type
$email_obj = WC()->mailer()->get_emails()[$email_key]; // Get specific WC_emails object
$heading_txt = __('Your Order Shipped','woocommerce'); // New heading text
return $email_obj->format_string( $heading_txt );
}
return $heading;
}
// Customize email subject for this custom status email notification
add_filter( 'woocommerce_email_subject_customer_processing_order', 'email_subject_customer_shipped_order', 10, 2 );
function email_subject_customer_shipped_order( $subject, $order ){
if( $order->has_status( 'shipped' ) ) {
$email_key = 'WC_Email_Customer_Processing_Order'; // The email notification type
$email_obj = WC()->mailer()->get_emails()[$email_key]; // Get specific WC_emails object
$subject_txt = sprintf( __('Your %s order shipped receipt from %s', 'woocommerce'), '{site_title}', '{order_date}' ); // New subject text
return $email_obj->format_string( $subject_txt );
}
return $subject;
}
代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。
推荐阅读
- optimization - 使用 Prolog 在线性时间内展平树
- vba - Selenium css 和 xpath 选择器
- python - Altair:修复交互式()图表的标题位置
- javascript - 在 HTML 标记的文本内容中查找单词/文本并用突出显示标记替换匹配项的可靠方法是什么?
- git - 一种说服 git 将捆绑包获取到活动分支的方法?
- csv - AWK:多列CSV数据的统计操作
- json - 如何访问 jsoncpp 值的键
- r - 创建一个函数以通过 R 中的方程循环列
- firebase - Firebase 自定义身份验证
- d3.js - 如何将动态数据文本放在d3中的矩形内