Send an Email notification to the admin for pending order status in WooCommerce
Asked Answered
U

3

8

In WooCommerce, when a customer goes to checkout from cart and submit the order, if the payment is not processed, the order is set to "pending" payment. The Admin doesn't received any email about.

I would like to send an email to Admin for this kind of orders. How can I do it?

Undressed answered 28/7, 2017 at 13:45 Comment(1)
woocommerce has A LOT of hooks, I'm sure one of them might be helpful: docs.woocommerce.com/wc-apidocs/hook-docs.htmlRigamarole
P
23

UPDATE 2 (Change from woocommerce_new_order to woocommerce_checkout_order_processed thanks to Céline Garel)

This code will be fired in all possible cases when a new order get a pending status and will trigger automatically a "New Order" email notification:

// New order notification only for "Pending" Order status
add_action( 'woocommerce_checkout_order_processed', 'pending_new_order_notification', 20, 1 );
function pending_new_order_notification( $order_id ) {

    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );

    // Only for "pending" order status
    if( ! $order->has_status( 'pending' ) ) return;

    // Send "New Email" notification (to admin)
    WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger( $order_id );
}

Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.


A more customizable version of the code (if needed), that will make pending Orders more visible:

// New order notification only for "Pending" Order status
add_action( 'woocommerce_checkout_order_processed', 'pending_new_order_notification', 20, 1 );
function pending_new_order_notification( $order_id ) {
    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );

    // Only for "pending" order status
    if( ! $order->has_status( 'pending' ) ) return;

    // Get an instance of the WC_Email_New_Order object
    $wc_email = WC()->mailer()->get_emails()['WC_Email_New_Order'];

    ## -- Customizing Heading, subject (and optionally add recipients)  -- ##
    // Change Subject
    $wc_email->settings['subject'] = __('{site_title} - New customer Pending order ({order_number}) - {order_date}');

    // Change Heading
    $wc_email->settings['heading'] = __('New customer Pending Order'); 
    // $wc_email->settings['recipient'] .= ',[email protected]'; // Add email recipients (coma separated)

    // Send "New Email" notification (to admin)
    $wc_email->trigger( $order_id );
}

Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.

This version Allow to customize the email heading, subject, add recipients...

Pietro answered 28/7, 2017 at 20:44 Comment(6)
dear this function looks very awesome but its not working for me.i am using wp 4.8 version.Undressed
@burhanjamil I have updated my answer, please try it. I have changed the hook and now it should work in any cases for "pending orders status"… Now the code is more effective, compact and lighter. There is 2 versions one that just send the default "new order" notification and the other one that will allow some customizations (if needed)…Pietro
Its not working. I have also test by creating simple.txt file either the hook is calling or not. Its call the function but can't trigger the order email.Nonprofit
Thank you works straight out of the box. Wordpress version 5.2.2Multiracial
Works great. Thank you. I just have one question? I would like to send email when order is pending and payment method is credit card. How could I do that?Sexagenary
@Pietro , does this code trigger once? once a day?Premarital
U
3

I've tried with LoicTheAztec answer, @LoicTheAztec thanks a lot for your great code.

I have just changed the action hook from woocommerce_new_order to woocommerce_checkout_order_processed to get it to work.

Here is the action : add_action( 'woocommerce_checkout_order_processed', 'pending_new_order_notification', 20, 1 );

Hope that helps.

Unquestionable answered 6/6, 2018 at 10:2 Comment(1)
Thanks for this fix! when I used woocommerce_new_order the order mail arrived without the products, but using woocommerce_checkout_order_processed fixed it, and the mail arrive as it sould :-)Hel
O
0

Thanks @LoicTheAztec – Here is a version of your code that sends the email after 10 minutes of the status being "Pending Payment".

It's utilising action scheduler and avoids the admin receiving pending emails when an order is only temporarily in that state such as for an external card payment.

Note the filter woocommerce_new_order_email_allows_resend which allows the new order email to be sent more than once.


add_filter( 'woocommerce_new_order_email_allows_resend', '__return_true' );

add_action( 'woocommerce_checkout_order_processed', 'pending_new_order_notification', 20, 1 );

function pending_new_order_notification( $order_id ) {
    $delayTime = 600;

    as_schedule_single_action(time() + $delayTime, 'queue_pending_email', array( $order_id ), 'customActions');
}

add_action( 'queue_pending_email', function( $order_id ) {
    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );

    // Only for "pending" order status
    if( ! $order->has_status( 'pending' ) ) return;
    
    // Get an instance of the WC_Email_New_Order object
    $wc_email = WC()->mailer()->get_emails()['WC_Email_New_Order'];

    ## -- Customizing Heading, subject (and optionally add recipients)  -- ##
    // Change Subject
    $wc_email->settings['subject'] = __('{site_title} - New customer pending order ({order_number}) - {order_date}');

    // Change Heading
    $wc_email->settings['heading'] = __('New Order Still Pending'); 
    // $wc_email->settings['recipient'] .= ',[email protected]'; // Add email recipients (coma separated)

    $wc_email->settings['additional_content'] = __('This order is currently pending.');

    // Send "New Email" notification (to admin)
    $wc_email->trigger( $order_id );
}, 20, 1 );
Outshout answered 11/8, 2023 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.