Email notification to the admin and customer for pending order - php

I am trying to make Email notification that goes to the admin and also to customer for a pending order.
I am using "Send an Email notification to the admin for pending order status in WooCommerce" answer code, that sends the email only to the admin and I want to sent it also to the customer.
Any help is appreciated.

You need to add also an email notification to customer as "On Hold" for example, like:
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 );
// Send "On Hold Email" notification (to customer)
WC()->mailer()->get_emails()['WC_Email_Customer_On_Hold_Order']->trigger( $order_id );
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Since WooCommerce 5+: Allow re-sending New Order Notification in WooCommerce 5+

Related

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

In WooCommerce, when a customer submit the order, the order can be set to "processing" payment. Or "on hold". 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?
Tryed this code (for "Processing" status):
// New order notification only for "Process" Order status
add_action( 'woocommerce_checkout_order_processed', 'process_new_order_notification', 20, 1 );
function process_new_order_notification( $order_id ) {
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// Only for "Process" order status
if( ! $order->has_status( 'process' ) ) return;
// Send "New Email" notification (to admin)
WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger( $order_id );
}
But something wrong and still no email
Help please

Receiving duplicated Woocommerce mail for completed order

I was planning to get Notified by mail when the customer has a pending order and I found out that I can do it this way:
Send an Email notification to the admin for pending order status in WooCommerce
The thing that surprised me is after using this on function.php, now I can only get Woocommerce admin mail when an order is pending, even after I deleted this code, it didn't change (not receiving admin mail for completed orders).
But it's ok I wanted the pending mail anyways.
So after this change now I'm not receiving completed order mails as admin and I have found the solution on :
Allow re-sending New Order Notification in WooCommerce 5+
The only problem I have now is that I'm getting duplicated completed order for admin (customer mails are fine)
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'] .= ',name#email.com'; // Add email recipients (coma separated)
// Send "New Email" notification (to admin)
$wc_email->trigger( $order_id );
}
add_action('woocommerce_order_status_completed', 'email_completed_order_admin_notification', 10, 2 );
function email_completed_order_admin_notification( $order_id, $order ) {
WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger( $order_id );
}
add_filter('woocommerce_new_order_email_allows_resend', '__return_true' );
add_filter('woocommerce_new_order_email_allows_resend', '__return_true' ); this one is making me receive completed order mails again but also making it duplicated.

Send Order On Hold Email for a specific shipping method in Woocommerce

I am using Wordpress 4.9.6 and WooCommerce version 3.4.3 and I need to send 'Order on Hold' email for a specific shipping method.
Reason?
I use DHL shipping plugin to calculate shipping and an 'Alternate' shipping method is also available. If the user chooses DHL shipping while checking out, the shipping cost is calculated and the order is good to go. However, if they choose the 'Alternate' shipping method, I got to inform them that their order is on hold till they pay for shipping because the 'Alternate' method is 'Free Shipping' renamed and I will issue a separate invoice for them to pay for shipping once they have ordered.
Searching for a solution to my problem, I have found some code that matches my needs in this answer thread: Customizing Woocommerce New Order email notification based on shipping method
But I am unable to figure out how to edit this code in order to make it work for my specific scenario.
Your help is greatly appreciated.
To make it work for a renamed free shipping method, you will need to change the code a bit:
add_action ('woocommerce_email_order_details', 'custom_email_notification_for_shipping', 5, 4);
function custom_email_notification_for_shipping( $order, $sent_to_admin, $plain_text, $email ){
// Only for "On hold" email notification and "Free Shipping" Shipping Method
if ( 'customer_on_hold_order' == $email->id && $order->has_shipping_method('free_shipping') ){
$order_id = $order->get_id(); // The Order ID
// Your message output
echo "<h2>Shipping notice</h2>
<p>Your custom message goes here… your custom message goes here… your custom message goes here… your custom message goes here… your custom message goes here…</p>";
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
Forcing "On hold" and "Completed" email notification (optional)
On orders status change the below code will trigger "On hold" email notification only for your renamed "Free shipping" shipping method and "Completed" email notification.
add_action( 'woocommerce_order_status_changed', 'sending_on_hold_email_notification', 20, 4 );
function sending_on_hold_email_notification( $order_id, $old_status, $new_status, $order ){
// Only "On hold" order status and "Free Shipping" Shipping Method
if ( $order->has_shipping_method('free_shipping') && $new_status == 'on-hold' ){
// Getting all WC_emails objects
$notifications = WC()->mailer()->get_emails();
// Send "On hold" email notification
$notifications['WC_Email_Customer_On_Hold_Order']->trigger( $order_id );
} elseif ( ! $order->has_shipping_method('free_shipping') && $new_status == 'completed' ){
// Getting all WC_emails objects
$notifications = WC()->mailer()->get_emails();
// Send "On hold" email notification
$notifications['WC_Email_Customer_Completed_Order']->trigger( $order_id );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.

Set Woocommerce new orders to "On hold" without going to "Processing"

Is it possible to set all new orders to on hold without them going to processing first?
I need to capture the funds, but the order needs to go as on-hold rather than processing so that I can make sure the payment is received before triggering the email to our supplier.
I have found some code that allows me to change all new orders to on-hold, however when payment is authorized the order is set to processing.
pending payment --> processing (triggers email) --> on-hold
Screenshot of Order Notes that show this
This is the code that I found:
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_onhold_order' );
function custom_woocommerce_auto_onhold_order( $order_id ) {
global $woocommerce;
if ( !$order_id )
return;
$order = new WC_Order( $order_id );
$order->update_status( 'on-hold' ); //All new orders go to "on-hold"
}
I've been looking for the action which triggers the order status to be set to processing on payment authorization however I cannot find it.
All help is greatly appreciated!
Edit: I've managed to stumble upon a snippet of code that appears to work.
add_filter( 'woocommerce_payment_complete_order_status', 'custom_update_order_status', 10, 2 );
function custom_update_order_status( $order_status, $order_id ) {
return 'on-hold';
}
All orders start with a "pending" status in Woocommerce. That status is set before they go through the payment gateway...
There is no email notifications for "Pending" orders staus
You can try the following, that will set the status "on-hold" on order creation (The update status action comes once the order has been created ans saved in database):
add_action( 'woocommerce_checkout_create_order', 'force_new_order_status', 20, 1 );
function force_new_order_status( $order ) {
if( ! $order->has_status('on-hold') )
$order->set_status( 'on-hold', 'Forced status by a custom script' );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

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?
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'] .= ',name#email.com'; // 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...
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.

Categories