Different email heading based on shipping methods in Woocommerce - php

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;
}

Related

Remove product purchase notes in certain WooCommerce email notifications

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

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";
}

Add Applied Coupon Code in Admin New Order Email Template - WooCommerce

Let me clear my question:
I have downloaded & activated WooCommerce Plugin for E-Commerce Functionality.
I want to add "Applied coupon code" in Admin New Order Email Template using my custom plugin.
Now:
Can you tell me that exact Hook or Function which is actually setting up that New Order Email Template so that i will override it?
Can you tell me how to call applied coupon code, so that i will display this in email template?
It would be great, if you help me please.
This can be done using a custom function hooked in woocommerce_email_order_details action hook (for example) that will display in admin emails notifications the used coupons in the order:
// The email function hooked that display the text
add_action( 'woocommerce_email_order_details', 'display_applied_coupons', 10, 4 );
function display_applied_coupons( $order, $sent_to_admin, $plain_text, $email ) {
// Only for admins and when there at least 1 coupon in the order
if ( ! $sent_to_admin && count($order->get_items('coupon') ) == 0 ) return;
foreach( $order->get_items('coupon') as $coupon ){
$coupon_codes[] = $coupon->get_code();
}
// For one coupon
if( count($coupon_codes) == 1 ){
$coupon_code = reset($coupon_codes);
echo '<p>'.__( 'Coupon Used: ').$coupon_code.'<p>';
}
// For multiple coupons
else {
$coupon_codes = implode( ', ', $coupon_codes);
echo '<p>'.__( 'Coupons Used: ').$coupon_codes.'<p>';
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works...
This is because $coupon_codes is not an array() define it with $coupon_codes=array();

How to add custom email notification in Woocommerce?

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);

Email conditional logic after order complete in WooCommerce

I'm trying to disable email notification when order total is equal to 0. It looks that my hook code works with this simple condition, but i don't know how to retrieve order details at this step.
$array = array('woocommerce_order_status_completed' );
function filter_woocommerce_email_actions( $array ) {
if (0>1){
return $array;
}
};
add_filter( 'woocommerce_email_actions', 'filter_woocommerce_email_actions', 10, 1 );
Or should i use other hook for this? Any ideas?
You can remove recipient from email notifications for new order based on order total. Use this piece of code and customize it based on order total for your requirement.
add_filter( 'woocommerce_email_recipient_new_order', 'wc_new_order_cash_on_delivery_recipient', 10, 2 );
function wc_new_order_cash_on_delivery_recipient( $recipient, $order ) {
if ( 'cod' == $order->payment_method ) {
$recipient .= ', john#example.com';
}
return $recipient;
}

Categories