• Hai,
    I create a new custom order status with code from an article. I made a custom status named “shipping’. This is the code

    // Register New Order Statuses
    function wpex_wc_register_post_statuses() {
        register_post_status( 'wc-shipping-progress', array(
            'label'                     => _x( 'Shipping', 'WooCommerce Order status', 'text_domain' ),
            'public'                    => true,
            'exclude_from_search'       => false,
            'show_in_admin_all_list'    => true,
            'show_in_admin_status_list' => true,
            'label_count'               => _n_noop( 'Approved (%s)', 'Approved (%s)', 'text_domain' )
        ) );
    }
    add_filter( 'init', 'wpex_wc_register_post_statuses' );
    
    // Add New Order Statuses to WooCommerce
    function wpex_wc_add_order_statuses( $order_statuses ) {
        $order_statuses['wc-shipping-progress'] = _x( 'Shipping', 'WooCommerce Order status', 'text_domain' );
        return $order_statuses;
    }
    add_filter( 'wc_order_statuses', 'wpex_wc_add_order_statuses' );

    But I need an email notification that should be sent to the customer and admin when we change the order status to ‘Shipping’, just like another email notification from basic woocommerce status order. Anyone can help, please? I wish there is an answer without installing a plugin.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Ok, this is not a complete solution but might help a bit – you can trigger Woocommerce emails like this: WC()->mailer()->emails['WC_Email_Customer_Processing_Order']->trigger($orderId);


    http://wordpress.stackexchange.com/questions/191860/woo-commerce-send-mail-by-code

    Thread Starter hrtliah

    (@hrtliah)

    Hi Andreas,
    Thank you for your answer, but i’m really a novice and i’m not a coder. Can you give me a little explanation? I don’t know where to put that code and what it means to trigger woocommerce emails.

    Yes, I will try to 🙂
    The code you mention in the opening post, where exactly in which file is it located?

    Thread Starter hrtliah

    (@hrtliah)

    Hi Andreas,
    I put that in my functions.php file in my bazar theme folder. I’m using bazar theme right now.

    Sorry, took bit longer..
    Please add this also to your “functions.php” of your active theme:

    function my_awesome_shipping_notification( $order_id, $checkout=null ) {
       global $woocommerce;
    
       $order = new WC_Order( $order_id );
    
       //error_log( $order->status );
    
       if($order->status === 'shipping-progress' ) {
    
          // Create a mailer
          $mailer 		= $woocommerce->mailer();
    
          $message_body = __( 'Your order is being shipped..', 'text_domain'  );
    
          $message 		= $mailer->wrap_message(
            // Message head and message body.
            sprintf( __( 'Order %s ready for shipping', 'text_domain'  ), $order->get_order_number() ), $message_body );
    
          // Client email, email subject and message.
    		$result = $mailer->send( $order->billing_email, sprintf( __( 'Order %s received', 'text_domain'  ), $order->get_order_number() ), $message );
    
    	 //error_log( $result );
    	}
    
    }
    add_action( 'woocommerce_order_status_changed', 'my_awesome_shipping_notification');

    If that is the most recommended code for the most recent Woocommerce version I’m not sure. But when I tried it out I received an email when order status was set on “Shipping”, so it worked for me.*

    Btw since checking if mailer is really working is a bit tricky:
    Add this to wp-config.php

    define('WP_DEBUG', true);
    define('WP_DEBUG_LOG', true);
    define('WP_DEBUG_DISPLAY', false);

    The output of error_log( $variable ) will then appear in your-install.com/wp-content/debug.log If you test on localhost like Xampp or WAMP use plugin WP-Mail-SMTP and connect to e.g. to a Google mail account.


    *http://stackoverflow.com/questions/27112461/woocommerce-send-custom-email-on-custom-order-status-change -> user “quelicm”

    Hi Andreas

    >>WC()->mailer()->emails[‘WC_Email_Customer_Processing_Order’]->trigger($orderId);

    You saved my life, I have been looking for this code for days and it almost drives me crazy.

    Glad to hear 🙂
    Btw just to mention, there is a down-side with adding code additions to functions.php of theme. Theme update => additions gone(very likely).
    Would be best to put extra code into custom plugin, or https://wordpress.org/plugins/code-snippets/

    Hi Andreas

    Addded to fucntions.php which is working fine, can we add reply-to tag to the mailer function ?

    // Client email, email subject and message.
    $result = $mailer->send( $order->billing_email, sprintf( __( ‘Order %s received’, ‘text_domain’ ), $order->get_order_number() ), $message );

    Thanks a lot for your help, it really saved me.

    I created a custom status “was shipped” and the mail is going out.

    At the same time I would like to use the functionality of another plugin that appends the tracking info to the outbound mail automaticaly.

    The plugin uses the hook “woocommerce_email_before_order_table“.

    How can I include the hook or just use the custom mail template alltogether.
    Do you happen to know if it is possible to assign the template somehow in the mailer process?

    I saw something like

    $this->template_html  = 'emails/customer-tracking.php';
    $this->template_plain = 'emails/plain/customer-tracking.php';

    but it was used in a public function __construct() {}

    thanks a lot in advance

    Hi there,
    I have found this topic through the search function and might have a very easy question. To clearify – I am not a coder and I am not located in the development. I received a customer request which can not be answered by the webshop guys and that’s why I am here 😉

    Current status: we do use WooCommerce for a customer’s webshop which was implemented a few weeks ago. So before the order status is sent to the customer do you know if it possible to have a pre-information? It should inform the customer about the receipt of the order and that we do check the current stock and product level. Might be a stupid question but if the code above can be used for any status message, it might be also possible to take it for a pre-status information.

    Thanks for any help on this in advance!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Email notification for custom order status’ is closed to new replies.