Remove product purchase notes in certain WooCommerce email notifications - php

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

Related

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.

Different email heading based on shipping methods in Woocommerce

So I want to send the same email with a slightly different heading in WooCommerce. It uses the argument $email_heading variable to hold the value of that current email heading so I figure a simple replacement would work. It doesn't. I could be completely missing the mark here any help is much appreciated.
I want it to say Your Order is ready for pickup when they choose local pickup and then the default value (stored the email settings of woocommerce) when any other shipping is chosen.
add_filter( "woocommerce_email_heading_customer_completed_order", 'HDM_woocommerce_email_header', 10, 2 );
function HDM_woocommerce_email_header( $email_heading, $email ) {
if ('customer_completed_order' == $email->id && $order->has_shipping_method('local_pickup') ){
$order_id = $order->get_id();
$email_heading = '<h1>Your Order Is Ready For Pickup</h1>';
}
return $email_heading;
};
There are some mistakes in your code like $email that is in fact $order. Also you are already targeting customer_completed_order in this composite hook, so you don't need it in your IF statement…
So try instead:
add_filter( "woocommerce_email_heading_customer_completed_order", 'custom_email_heading', 10, 2 );
function custom_email_heading( $email_heading, $order ) {
if ( $order->has_shipping_method('local_pickup') ){
$email_heading = '<h1>Your Order Is Ready For Pickup</h1>';
}
return $email_heading;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
i'm wondering if this is valid for email subject:
add_filter( "woocommerce_email_subject_customer_completed_order", 'custom_email_subject', 10, 2 );
function custom_email_subject( $subject, $order ) {
if ( $order->has_shipping_method('local_pickup') ){
$subject = 'Your Order Is Ready For Pickup';
}
return $subject;
}

Conditional custom New Order Woocommerce email notification on different recippients

Okay I have had a question come from a client that doesn't seem impossible, but correctly setting up the conditions is a problem for me. Here is what is happening and what I need. i would really this to be a function of possible.
The store has a user hierarchy. There are 2x Super Admins (ID: admin), 5x Admins (ID: admin2), 15x Bank Customers (ID: banks), and 2x Real Estate customers (ID: real estate). I have already setup the custom users with the ID's. No products have pricing. Everything is invoiced in an email with the order_info. Everything is paid at a later date in house. So the site really relies on emails.
If the 15x Bank Customers place a new_order, these new_order(s) need to send the email invoice to the 5x Admins.
If the 2x Real Estate customers place a new_order, these new_order(s) need to send the email invoice to the 5x Admins.
If the 5x Admins place a new_order, these new_order(s) need to send the email invoice to the 2x Super Admins.
I am not the greatest with php and what I had previous was very sloppy.
Any help?
You can use wc hook as follows in functions.php filr
add_action( 'woocommerce_new_order', 'send_email_to_admin', 1, 1 );
function send_email_to_admin($order_id) {
// use order id to find user id and send email
}
Using a custom function hooked in woocommerce_thankyou action hook, will allow you to make that conditional email notifications based on user roles and on custom multiple recipients.
You will need to replace admins and super admins email in this function.
You will need also to check that the users roles are matching in the 3 if statements… The ID for Real estate can't have a space normally and should be instead: 'real_estate'
This will send email notifications for orders that have a status like 'on-hold', 'pending', 'processing' or 'completed'...
Once this custom email will be triggered a custom field '_custom_emails_sent' will be set for the order.
Here is the code:
add_action( 'woocommerce_thankyou', 'custom_new_order_email_notifications', 10, 1 );
function custom_new_order_email_notifications( $order_id ){
// If Custom Emails already sent we exit
if( get_post_meta( $order_id, '_custom_emails_sent', true ) ) return;
$targeted_statuses = array( 'wc-on-hold', 'wc-pending', 'wc-processing', 'wc-completed' );
$order_status = get_post_status( $order_id );
// Only for the correct order statuses; If not we exit
if( ! in_array( $order_status, $targeted_statuses ) ) return;
// HERE (below) replace super admins and admins REAL emails
$super_admin_emails = array(
'supadmin1#example.com', 'supadmin2#example.com' );
$admin_emails = array(
'admin1#example.com', 'admin2#example.com',
'admin3#example.com', 'admin4#example.com', 'admin5#example.com' );
// Get the user of the order
$user_id = get_post_meta( $order_id, '_customer_user', true );
$user = get_userdata( $user_id );
$recipient = '';
// 1. Bank Customers user role
if( in_array('banks', $user->roles) ){
$recipients = implode(',', $admin_emails);
}
// 2. Real estate Customers user role
if( in_array('real_estate', $user->roles) ){
$recipients = implode(',', $admin_emails);
}
// 3. Admins Customers user role
if( in_array('admin2', $user->roles) ){
$recipients = implode(',', $admin_emails);
}
// Sending new order email notification to the targeted recipients
if( '' != $recipients ){
$mailer = WC()->mailer()->get_emails();
$mailer['WC_Email_New_Order']->recipient = $recipients;
$mailer['WC_Email_New_Order']->trigger( $order_id ); // sending
// We set a custom field that will avoid repetitive sends
update_post_meta( $order_id, '_custom_emails_sent', '1' );
}
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works

Send Woocommerce Order to email address listed on product page

I'm using Woocommerce and the Product Addons plugin to add extra fields to a product. One of those fields is an email address to let people send the confirmation of the order to a DIFFERENT address than the billing address shown on the checkout page. Emails should be sent to both addresses.
Any thoughts on maybe how to modify the functions.php file to do this?
In the woocommerce_email_recipient_{$this->id} filter hook, you can use the $order argument to get your 2nd email.
But first Lets add globally an email field with the Product Add-ons plugin…
The add on field on the product (fill the field and add to cart):
This "Email" field in order-received (Thank you) page, after checkout:
As you can notice the label of this field is "Email"…
Now if I look in the database in wp_woocommerce_order_itemmeta for this order I can see for the meta_key "Email" the meta_value "loic#TheAztec.com" :
Now I can set the correct meta_key in the code below to get my email.
Here is the code that will add this additional email recipient for processing and completed customer order email notifications:
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'additional_customer_email_recipient', 10, 2 ); // Processing Order
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'additional_customer_email_recipient', 10, 2 ); // Completed Order
function additional_customer_email_recipient( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
$additional_recipients = array(); // Initializing…
// Iterating though each order item
foreach( $order->get_items() as $item_id => $item_data ){
// HERE set the the correct meta_key (like 'Email') to get the correct value
$email = wc_get_order_item_meta( $item_id, 'Email', true );
// Avoiding duplicates (if many items with many emails)
// or an existing email in the recipient
if( ! in_array( $email, $additional_recipients ) && strpos( $recipient, $email ) === false )
$additional_recipients[] = $email;
}
// Convert the array in a coma separated string
$additional_recipients = implode( ',', $additional_recipients);
// If an additional recipient exist, we add it
if( count($additional_recipients) > 0)
$recipient .= ','.$additional_recipients;
return $recipient;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works.
You can add below code in your function.php
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'your_email_recipient_filter_function', 10, 2);
function your_email_recipient_filter_function($recipient, $object) {
$recipient = $recipient . ', me#myemail.com';
return $recipient;
}
and if you want to send email in BCC then please try below code:
add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 2);
function mycustom_headers_filter_function( $headers, $object ) {
if ($object == 'customer_completed_order') {
$headers .= 'BCC: My name <my#email.com>' . "\r\n";
}

Changing in WooCommerce email notification Order Item table "Product" label

I need to change the text (label) "Product" to "Ticket" in WooCommerce Order Item table email notifications.
How would I do this?
Is it possible?
Thanks
First we need to get the Email ID to target the all email notifications. The only way is to get it before and to set the value in a global variable.
Then in a custom function hooked in Wordpress gettext action hook, we can change (translate) "Product" in all email notifications.
Here is that code:
## Tested on WooCommerce 2.6.x and 3.0+
// Setting the email_is as a global variable
add_action('woocommerce_email_before_order_table', 'the_email_id_as_a_global', 1, 4);
function the_email_id_as_a_global($order, $sent_to_admin, $plain_text, $email){
$GLOBALS['email_id_str'] = $email->id;
}
add_filter('gettext', 'wc_renaming_email_label', 50, 3);
function wc_renaming_email_label( $translated_text, $untranslated_text, $domain ) {
// Getting the email ID global variable
$refNameGlobalsVar = $GLOBALS;
$email_id = $refNameGlobalsVar['email_id_str'];
if( !is_admin() && $email_id ) {
if( $untranslated_text == 'Product' )
$translated_text = __( 'Ticket', $domain );
}
return $translated_text;
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested on WooCommerce from 2.6.x to 3.0+ and works.
If you don't want to modify the WooCommerce files, use this plugin https://wordpress.org/plugins/woo-custom-emails/
If you want to edit it from the WooCommerce files, then modify the email templates in /wp-content/plugins/woocommerce/templates/emails/

Categories