Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 510
burhan jamil Avatar asked Jul 28 '17 13:07

burhan jamil


People also ask

How do I add email alerts to WooCommerce?

Double-check that “Enable this email notification” is ticked for order notifications at WooCommerce > Settings > Emails and select the Processing Order email template. An additional test should be setting the Email Type to plain text.

What does pending payment mean in WooCommerce?

When taking payments via WooCommerce (e.g. via PayPal), your “Orders” page will show the following status labels: Pending payment: This means the order has been created on your website, but the customer's payment hasn't been received yet.

Why are WooCommerce orders on hold?

In WooCommerce on hold status is triggered when the order is placed, the stock is reduced but the store is awaiting payment.


Video Answer


2 Answers

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...

like image 61
LoicTheAztec Avatar answered Sep 29 '22 01:09

LoicTheAztec


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.

like image 39
Céline Garel Avatar answered Sep 29 '22 01:09

Céline Garel