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.
Related
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
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.
I try to remove the purchase notes from the order completed mail. Currently the purchase notes are in the order confirmation mail and also in the order completed mail. But we do not want that info in the order completed mail.
So for that I found this input here: https://wordpress.stackexchange.com/questions/340045/how-do-i-hide-the-purchase-note-in-the-woocommerce-order-completed-email
I also found that snipped here below. But with that it removes it everywhere and not just in the order complete mail. Any advice?
add_filter('woocommerce_email_order_items_args','remove_order_note',12);
function remove_order_note($args){
$args['show_purchase_note']= false;
return $args;
}
You can use the email ID to target specific email notifications in WooCommerce.
However, because this is not known in the hook you are using, we will make the email id globally available via another hook
So you get:
// Setting the email_is as a global variable
function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
$GLOBALS['email_id_str'] = $email->id;
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 10, 4 );
function filter_woocommerce_email_order_items_args( $args ) {
// Getting the email ID global variable
$refNameGlobalsVar = $GLOBALS;
$email_id = isset( $refNameGlobalsVar['email_id_str'] ) ? $refNameGlobalsVar['email_id_str'] : '';
// Targeting specific email. Multiple statuses can be added, separated by a comma
if ( in_array( $email_id, array( 'customer_completed_order', 'customer_invoice' ) ) ) {
// False
$args['show_purchase_note'] = false;
}
return $args;
}
add_filter( 'woocommerce_email_order_items_args', 'filter_woocommerce_email_order_items_args', 10, 1 );
Note: How to target other WooCommerce order emails
Used in this answer: Changing in WooCommerce email notification Order Item table "Product" label
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+
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.