Search code examples
phpwordpresswoocommercephpmailer

Send additional email in WooCommerce in a processing status order (functions.php)


I need to send these two emails after the checkout.

1.- Woocommerce automatic email (summerizing the order, prince, total price, etc).

2.- Email with aditional information.

I have been looking and I can't do it from WooCommerce plugin and I can't use other plugins like Shopmagic, so ... It has to be with code.

After than a lot of time searching, I think could be something like this.

(The order of status control is processing and no completed because I'm testing with test-mode in Stripe and Stripe, in test mode, orders are defined as "processing" status, not completed.

What I have from now in my functions.php file is this:

$order = new WC_Order( $order_id );

function order_processing( $order_id ) {
    $order = new WC_Order( $order_id );
    $to_email = $order["billing_address"];
    $headers = 'From: Your Name <alexiglesiasvortex@gmail.com>' . "\r\n";
    wp_mail($to_email, 'subject', '<h1>This is a test for my new pending email.</h1><p>Agree, this is a test</p>', $headers );
}

add_action( 'woocommerce_payment_processing', 'order_processing' );

Currently, this is not working ... I don't get any error and checkout ends correctly, but no new email arrives to my inbox.

I need some help, guys, someone can help me? Thank you a lot and have a good monday!


Solution

  • I couldn't find any woocommerce_payment_processing hooks in the WooCommerce documentation. Try woocommerce_order_status_changed instead.

    Also you are accessing the billing email incorrectly. $order is an object and you cannot get the billing email as if it were an array. You can use the get_billing_email method of the WC_Order class.

    // send a custom email when the order status changes to "processing"
    add_action( 'woocommerce_order_status_changed', 'send_custom_email_order_processing', 10, 4 );
    function send_custom_email_order_processing( $order_id, $from_status, $to_status, $order ) {
    
        // only if the new order status is "processing"
        if ( $to_status == 'processing' ) {
            $to_email = $order->get_billing_email();
            $headers = 'From: Your Name <alexiglesiasvortex@gmail.com>' . "\r\n";
            wp_mail( $to_email, 'subject', '<h1>This is a test for my new pending email.</h1><p>Agree, this is a test</p>', $headers );
        }
    
    }
    

    The code has been tested and works. Add it to your active theme's functions.php.