I'm using WooCommerce for my website. I need to add custom order status and send custom content in email for custom status.I used the following code and created my own order status called Shipped, but my main problem is that when I change order status to Shipped, the email is sent with Completed Order content. how can I have my own content in the Shipped order status?
function register_brethren_order_statuses(){
register_post_status( 'wc-shipped', array(
'label' => 'Shipped',
'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>' )) );
}
add_action( 'init', 'register_brethren_order_statuses' );
// Add Status to WooCommerce Status List
function add_brethren_order_statuses( $order_statuses ){
$new_order_statuses = array();
// Add New Status After Processing
foreach ( $order_statuses as $key => $status ){
$new_order_statuses[ $key ] = $status;
if ( 'wc-processing' === $key ){
$new_order_statuses['wc-shipped'] = 'Shipped';
}
}
return $new_order_statuses;
}
add_filter( 'wc_order_statuses','add_brethren_order_statuses' );
add_action('woocommerce_order_status_shipped','bbloomer_status_custom_notification', 20, 2 );
function bbloomer_status_custom_notification( $order_id, $order ) {
$heading = 'Shipped';
$subject = 'Shipped';
// Get WooCommerce email objects
$mailer = WC()->mailer()->get_emails();
$mailer['WC_Email_Customer_Completed_Order']->heading = $heading;
$mailer['WC_Email_Customer_Completed_Order']->settings['heading'] = $heading;
$mailer['WC_Email_Customer_Completed_Order']->subject = $subject;
$mailer['WC_Email_Customer_Completed_Order']->settings['subject'] = $subject;
// Send the email with custom heading & subject
if ( $order->get_status() == "shipped" ) {
$mailer['WC_Email_Customer_Completed_Order']->trigger( $order_id );
}
}
add_action( 'woocommerce_email_before_order_table',
'bbloomer_add_content_specific_email', 20, 4 );
function bbloomer_add_content_specific_email( $order, $sent_to_admin,
$plain_text, $email ) {
if ( $order->get_status() == "shipped" ) {
echo '<h2 class="email-upsell-title">Shipped order</h2>Hi your order is shipping by post<p class="email-upsell-p"></p>';
}
}