0
votes

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>';
    }
}
1

1 Answers

2
votes

For the question it seems that Customer Completed Order email is getting executed. So there are two way to add your own custom content.

1) By overriding the customer completed order email tempalte from the WooCommerce plugin to the theme folder and doing necessary changes as per requirement.

2) Using hooks which are are related to email table formatting. For this see below snippet, which will add the content before the table body for the email template whose id is customer_completed_order and status is shipped

<?php
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 ( $email->id == 'customer_completed_order' && $order->get_status() == "shipped") { /*As customer completed order is getting executed and need to modify for the specific email*/
      echo '<h2 class="email-upsell-title">Lorem Ipsum sit Amet</p>';
   }
}
?>

Possible email template ids you can check with

<?php
if ( $email->id == 'cancelled_order' ) {}
if ( $email->id == 'customer_completed_order' ) {}
if ( $email->id == 'customer_invoice' ) {}
if ( $email->id == 'customer_new_account' ) {}
if ( $email->id == 'customer_note' ) {}
if ( $email->id == 'customer_on_hold_order' ) {}
if ( $email->id == 'customer_refunded_order' ) {}
if ( $email->id == 'customer_reset_password' ) {}
if ( $email->id == 'failed_order' ) {}
if ( $email->id == 'new_order' ) {}
?>

Hope you can play with this stuff and get success for sure.