How to add custom email notification in Woocommerce? - php

As title mentioned, is there any possible way to add email notification just like email to customer on cancelled order.
I would like to email to notice on cancelled order due to time limit of pending payment.
Any possible way??

Here is a custom hooked function in woocommerce_email_recipient_cancelled_order filter hook:
add_filter( 'woocommerce_email_recipient_cancelled_order', 'adding_customer_email_recipient_to_cancelled', 10, 2 );
function adding_customer_email_recipient_to_cancelled( $recipient, $order ){
if( is_admin() ) return $recipient;
$billing_email = $order->get_billing_email();
$recipient .= ', ' . $billing_email;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works on WooCommerce 3.0+

You can use action hook woocommerce_order_status_cancelled, this action is executed when order status is changed to cancelled.
In example:
add_action('woocommerce_order_status_cancelled', function($order_id){
//send email here
}, 10, 1);

Related

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.

Which hook to add custom data In New Order Admin Woocommerce email

In WooCommerce, How and where I can apply some code for New Order Email notification, where I should get the Applied Coupon Code display.
In the template for New Order Email notification I have #hooked WC_Emails::order_details() that shows the order details table…
Also is WC_Email::order_details() the exact hook which I am searching for to update coupon code in New order mail to admin?
I am loosed, any help will be really appreciated…
The hook that you are searching is woocommerce_email_order_details action hook that can be used this way:
add_action( 'woocommerce_email_order_details', 'action_email_order_details', 10, 4 );
function action_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
if( $sent_to_admin ): // For admin emails notification
// Your code goes HERE
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
In your function you can use any of the 4 arguments from the hook like $order the WC_Order object.
You can use the WC_Abstract_Order method get_used_coupons() that will give you an array of coupons codes used in the order, like:
// Get the array of coupons codes
$coupon_codes = $order->get_used_coupons();
// Convert and display the array in a coma separated string:
echo 'Coupon codes: ' . implode( ', ', $coupon_codes );

Remove shipping info for free shipping method on WooCommerce email notifications

When the customer order is placed, I would like if free shipping has been used, to remove shipping info from the order confirmation mail to the customer.
Is it possible to achieve this?
This is possible with this custom hooked function (but from all email notifications):
add_filter( 'woocommerce_get_order_item_totals', function( $total_rows, $order, $tax_display ){
// Only for "Free Shipping" method
if( ! $order->has_shipping_method('free_shipping') || is_account_page() || is_wc_endpoint_url( 'order-received' ) )
return $total_rows;
unset($total_rows['shipping']);
return $total_rows;
}, 11, 3 );
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.

Change WooCommerce processing order email notification recipient to a custom address

When the order status in WooCommerce is set from pending to processing I can send an email to my customer. But I need to send an email only to a custom email adresse instead of the customer default address.
Is there a hook or filter for the functions.php file?
This custom function hooked in woocommerce_email_recipient_customer_processing_order filter hook, will do the job (Set in it your replacement email):
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'processing_order_replacement_email_recipient', 10, 2 );
function processing_order_replacement_email_recipient( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
// Set HERE your replacement recipient email(s)… (If multiple, separate them by a coma)
$recipient = 'name#example.com';
return $recipient;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works
To add the custom recipient to the customer email, you should use this instead (if needed):
$recipient .= ',name#example.com';

Customizing customer processing emails notification in WooCommerce

I am very new to WordPress, and have created an e-commerce store with WooCommerce.
After the customer places an order, I get an email and the customer get an email- one for me to say what they have ordered, and one to them as a thank you email.
Within this thank you email, in my functions.php file, I have learned to change the subject of the header to include their name such as this:
//add the first name of the person to the person getting the reciept in the subject of the email.
add_filter('woocommerce_email_subject_customer_processing_order','UEBC_change_processing_email_subject', 10, 2);
function UEBC_change_processing_email_subject( $subject, $order ) {
global $woocommerce;
$subject = 'Thanks for your ' . get_bloginfo( 'name', 'display' ) . ' Order, '.$order->billing_first_name .'!';
return $subject;
}
The code snippet above works correctly, and is only displayed to the customer, not to me. e.g. "thanks for your order ABCD Clothes order John!".
Within the body of the email, I am trying to make this personal as a small thank you message, however, when I make the message, I am using the hook:
add_action( 'woocommerce_email_before_order_table', 'custom_add_content', 20,1 );
I know that since im using the woocommerce_email_before_order_table hook, the custom function will be send in the body of the email to both the customer and myself.
I was wondering, is there a hook that Woocommerce provides so that the custom function will only be sent to the customer within the body of the email?
For example: woocommerce_email_header_customer_processing_order or words to that effect?
Thanks
To add some custom content using woocommerce_email_before_order_table hook and targeting just the customer "processing order" email notification, you should try this:
add_action( 'woocommerce_email_before_order_table', 'custom_content_to_processing_customer_email', 10, 4 );
function custom_content_to_processing_customer_email( $order, $sent_to_admin, $plain_text, $email ) {
if( 'customer_processing_order' == $email->id ){
// Set here as you want your custom content (for customers and email notification related to processing orders only)
echo '<p class="some-class">Here goes your custom content… </p>';
}
}
Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
This code is tested and works

Categories