问题
i have created the custom status shipped but unfortunately mail are send default of processing when we change order status to shipped not the one we define in fuction shipped_status_custom_notification , can you please help me
/**
* 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;
}
回答1:
WooCommerce has changed a lot through all plugin updates since 2018, so to get an email notification for your custom order status with custom heading and subject use the following instead:
// 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;
}
Code goes in function.php file of your active child theme (or active theme). Tested and Works.
来源:https://stackoverflow.com/questions/63170962/custom-heading-and-subject-email-notification-for-custom-order-status-in-woocomm