Receiving duplicated Woocommerce mail for completed order - php

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.

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

Email notification to the admin and customer for pending order

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+

WooCommerce: Send E-Mail after order paid not working

I'm currently trying to send an email to my customer after he paid for an order but the email don't gets sent:
add_filter( 'woocommerce_payment_complete_order_status', 'update_order_status', 10, 2 );
function update_order_status( $order_status, $order_id ) {
do_action( 'woocommerce_order_status_pending_to_processing_notification', $order_id );
return 'completed';
}
I need to do this because I want to send the invoice and the payment notification email which is adapted to this filter.
The email I need to send
But when I complete an order the email dont gets sent.
The E-Mail I need to sent is activated in the WooCommerce settings:
Order completed status come so you need to use woocommerce_order_status_completed filter.
function woocommerce_order_status_completed_email( $order_id ) {
// here add your email code.
}
add_action( 'woocommerce_order_status_completed', 'woocommerce_order_status_completed_email', 10, 1 );
This will work for you.

Custom Email Missing products and Subtotal

I have a custom email that I created on New Orders. The aim is to let the client & the admin know that a new order has been finished (not paid). For that reason this order will be in pending. So this Email triggers when a New Order is created but when the administrator receives the email, products don't show up neither the correct subtotal. The total is right.
I am using a custom payment module but I think this is not the problem.
// New order notification only for "Pending" Order status
add_action( 'woocommerce_new_order', 'pending_new_order_notification', 20, 1 );
function pending_new_order_notification( $order_id ) {
//global $product;
// Get an instance of the WC_Order object
$order = new WC_Order( $order_id );
$items = $order->get_items();
// Only for "pending" order status
if( ! $order->has_status( 'pending' ) ) return;
$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 manual 2 order ({order_number}) - {order_date}');
// Change Heading
$wc_email->settings['heading'] = __('New customer Pending Order 2');
$wc_email->recipient .= ", $order->billing_email"; // Add email recipients (coma separated)
// Send "New Email" notification (to admin)
$wc_email->trigger( $order_id, $order );
//WC()->mailer()->emails['WC_Email_New_Order']->trigger( $order->get_id(), $order );
}
Same here.
I've tried with woocommerce_checkout_order_processed hook and it works.
Here is the action : add_action( 'woocommerce_checkout_order_processed', 'pending_new_order_notification', 20, 1 );
Hope that helps.

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