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.
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'm running into a problem with a custom function in a hook of WooCommerce orders status.
All I need is to show the order id, a custom field and the items of the order in error_log
function mysite_woocommerce_order_complete( $order_id ) {
error_log( "Payment has been received for order $order_id" );
$order = wc_get_order( $order_id );
$user_acct = get_post_meta( $order_id, 'billing_acc', true );
foreach ($order->get_items() as $item_id => $item_data) {
$product = $item_data->get_product();
$product_name = $product->get_name();
$item_quantity = $item_data->get_quantity();
error_log( "Product $product_name Quantity $item_quantity User $user_acct" );
}
}
add_action( 'woocommerce_order_status_completed', 'mysite_woocommerce_order_complete', 1, 1 );
This works perfectly when a customer complete payment and the product is virtual and downloadable, the order gets the status "Completed" and print what I need in error.log
[07-Jul-2020 20:18:53 UTC] Payment has been received for order 2846
[07-Jul-2020 20:18:53 UTC] Product Pacote Jered's 2 Quantity 2 User b1n
The problem is: My store accept a method of payment (bank transfer) and after the payment is verified by the admin, he needs to manually set the order as Completed.
In this action the hook isn't called, I need it to run just like the other way.
I've tried a lot of similar hooks, all ends with the same problem, it works if the order gets completed automatically but doesn't work if the order is completed manually.
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.