I'm trying to include a custom field in the email that is sent to customers upon purchase. I added a custom field "Phone_number" into the all of the product pages.
I've tried including
<?php
$orderidno = $order->id;
$prodmetaa = get_post_meta($orderidno, "Phone_number" , True);
echo $prodmetaa;
?>
In the order email template, but haven't had any success. Has anyone done anything like this before or have any Idea how I could accomplish this .
Plugin URL - https://wordpress.org/plugins/woocommerce/
You can use hooks/actions/filters to do so. Here is documentation on doing something like you want http://docs.woothemes.com/document/add-a-custom-field-in-an-order-to-the-emails/
/**
* Add the field to order emails
**/
add_filter('woocommerce_email_order_meta_keys', 'my_woocommerce_email_order_meta_keys');
function my_woocommerce_email_order_meta_keys( $keys ) {
$keys['How did you hear about us?'] = 'hear_about_us';
return $keys;
}
To expand upon the above tutorial and now taking advantage of the improved email template hooks that I added and I think were released in WC 2.3.0
function so_229971110_add_meta_to_email( $order, $sent_to_admin, $plain_text ){
$prodmetaa = get_post_meta($order->id, "Phone_number" , True);
echo "Phone Number: " . $prodmetaa;
}
add_action( 'woocommerce_email_customer_details', 'so_229971110_add_meta_to_email', 20, 3 );
Related
Email notification of New order process has 'My Blog' title.
I look into Woocommerce setting but could not find it.
Any Idea how to change 'My Blog' to 'X Company'
red underline text in attached images.
Plateform: Wordpress + Woocommerce
Update:
What you want to change is the "From name" and it can be changed using:
add_filter('woocommerce_email_from_name', 'change_new_order_email_from_name', 10, 2 );
function change_new_order_email_from_name( $from_name, $email ){
if( $email->id === 'new_order' )
$from_name = __("ACME corp");
return $from_name;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Addition: To add custom placeholders for the email subject (for woocommerce 3.2+):
// Only for woocommerce versions 3.2 + (up to 3.2)
add_filter( 'woocommerce_email_format_string' , 'custom_email_format_string', 20, 2 );
function custom_email_format_string( $string, $email ) {
// Get the instance of the WC_Order object
$order = $email->object;
// Additional wanted placeholders in the array of find / relace pairs
$additional_placeholders = array(
'{shop_company}' => __("ACME corp"),
);
// return the clean string with new replacements
return str_replace( array_keys( $additional_placeholders ), array_values( $additional_placeholders ), $string );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Then in your email settings, on the Subject field of an email notification you will be able to replace for example:
Your {site_title} order receipt from {order_date}
by
Your {shop_company} order receipt from {order_date}
Please use this WooCommerce hook woocommerce_email_subject_new_order to change new order email title.
add_filter( 'woocommerce_email_subject_new_order', 'customizing_new_order_subject', 10, 2 );
function customizing_new_order_subject( $formated_subject, $order ){
// Get an instance of the WC_Email_New_Order object
$email = WC()->mailer->get_emails()['WC_Email_New_Order'];
// Get unformatted subject from settings
$subject = $email->get_option( 'subject', $email->get_default_subject() );
// Loop through order line items
$product_names = array();
foreach( $order->get_items() as $item )
$product_names[] = $item->get_name(); // Set product names in an array
// Set product names in a string with separators (when more than one item)
$product_names = implode( ' - ', $product_names );
// Replace "{product_name}" by the product name
$subject = str_replace( '{product_name}', $product_names, $subject );
// format and return the custom formatted subject
return $email->format_string( $subject );
}
For more details see this link
Go to
WooCommerce > Settings > Emails > Processing Orders.
Here you will find a field called "Email Subject". Here, change {site_title} to whatever you want it to appear.
Alternatively, if you want to change the value of {site_title} itself, then head over to Settings > General.
Here you will find the field called Site Title. Change it to whatever you want it to appear.
Let me know if it works!
I found the solution after digging, its very easy.
WooCommerce>Setting>Emails
At the bottom, there is section where you can header and footer text.
Simple.
Really appreciate your help #LoicTheAztec
I'd like to add additional e-mail recipients depending on the customers post-code. We have different suppliers for different provinces (e.g. 4614 - Supplier 1, 3314 - Supplier 2) and it is necessary to direct the orders to the responsible suppliers. For accounting we need to direct the new orders to the responsible department (currently set in the WooCommerce backend).
I already tried to use the WooCommerce filter for the e-mail recipient to add the e-mails based on the post-code. For this purpose i set up two arrays and tried to get the shipping post-code (or the billing post-code) to use in this if-function.
This is the code i tried to use on our website:
add_filter( 'woocommerce_email_recipient_new_order', 'cond_recipients_email_notifications', 10, 2 );
function cond_recipients_email_notifications( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
// TARGET-ZIP-CODE
$zip_zone1 = array( '4614', '4072', '4615', '4064', '4062', '4611' );
$zip_zone2 = array( '3314', '3353', '3313', '3312', '3350', '3322', '3354' );
// User ZIP-CODE
$user_zip_zone = $order->get_shipping_postcode();
if(empty($user_shipping_postcode))
$user_zip_zone = $order->get_billing_postcode();
// ADD EMAIL IF ZIP-CODE MATCHES TARGET-ZIP-CODE
if ( in_array( $user_zip_zone, $zip_zone1)) {
$recipient .= ', e-mail-supplier1#e-mail.at';
} elseif ( in_array( $user_zip_zone, $zip_zone2) ) {
$recipient .= ', e-mail-supplier2#e-mail.at';
}
return $recipient;
}
I tried to implement this snippet in the functions.php of my child theme but it doesn't work as expected. I'm obviously missing something but i have no clue what my mistake could be. Unfortunately i have little experience with php so my understanding of this matter is limited.
Does anybody know a way to implement the needed function in WooCommerce or how to make this code-snippet work? What is wrong with my code?
Help is very appreciated!
I got the information that this code is working correctly. Upon getting this info i checked my server settings and there were some wrong settings.
The code works as it was supposed to be! Thank you for your help!
Feel free to use my code, maybe someone else needs to implement the same function.
I am keen to hook a section into the WooCommerce emails with the customer number (which is the Wordpress user ID).
If I insert the following code directly into the email template file
echo $order->user_id;
It works just fine, but if I try and hook it in, nothing is returned - except the word 'Hooked' which shows my hook is correct.
add_action( 'woocommerce_email_after_order_table', 'lnz_hook_content', 1);
function lnz_hook_content() {
echo 'hooked'; // Test Purposes only
echo $order->user_id;
Any thoughts? Replies much appreciated.
Sorted it. Needed to add the parameter into my new function:
add_action( 'woocommerce_email_after_order_table', 'lnz_hook_content', 1);
function lnz_hook_content( $order) {
echo 'hooked'; // Test Purposes only
echo $order->user_id;
Ok so I am building a woocommerce site for a client and they love it but the only issue is they need a customer number to come through with the email for all new orders. They are willing to go in and fill in this info for each user, but I cannot find out how to get this info to automatically send with the new order email.
I am using WP - Members plugin to add custom information to the users profile, and have created a field there called Customer # (option name: customer_number) I cannot figure how to get woocommerce to add this number to the email that is sent when they place the order (to the admin)
I have tried many different things, it seems like it should just be a hook but I dont know how to get the values or have not been able to figure this out yet in any way.
I have looked and looked and this is my last resort, my minimal understanding of php and hooks is probably my downfall here, so please be very clear if instructing me in that way.
I will not give up on this issue, so I hope someone can help!
We can adapt from my tutorial on customizing the checkout fields to display some extra info in the email:
function kia_display_email_order_user_meta( $order, $sent_to_admin, $plain_text ) {
$user_id = $order->customer_user;
if( ! empty( $user_id ) && ( $data = get_user_meta( $user_id, '_some_user_meta', true ) ) != '' ){
if( $plain_text ){
echo 'The user data for some field is ' . $data;
} else {
echo '<p>The user data for <strong>some field</strong> is ' . $data . '</p>';
}
}
}
add_action('woocommerce_email_customer_details', 'kia_display_email_order_user_meta', 30, 3 );
In this case we'll replace the get_post_meta() of my tutorial with get_user_meta().... and knowing that the user Id is stored in the $order->customer_id property of the $order object.
Basically, in woocommerce you have the option to input multiple email addresses (separated by commas) of who to send the completed order to, in WooCommerce -> Settings -> Emails -> New order. But I need a way to send to only one of these recipients based on the zip code of the customer who is ordering the product. Or completely overwrite woocommerce's way of handling this.
How can I tie into the function responsible for this, in order to send to the correct recipient? Basically, is there a hook defined for this, or does a plugin exist for something like this, or will I have to edit core WooCommerce files? If edits are needed to core files, can someone point me in the right direction as to which files will need edits?
I had a bit of trouble with helgatheviking's answer above and also a slightly different use case.
My problems/needs were:
I didn't quite understand the filter name as presented above.
The public function get_recipient() inside of class-wc-email.php expected a string but was getting an array.
A also wanted to conditionally add the extra recipient based on the payment method, rather than postcode.
Here's what I did instead:
Added full filter name, not just the suffix: woocommerce_email_recipient_new_order
Replaced explode() and array_push() with string concatenation $email .= ',' . $additional_email;.
Conditionally checked payment method: if( $order->get_payment_method() == "cod" ).
Full example:
add_filter( 'woocommerce_email_recipient_new_order' , 'so_26429482_add_recipient', 20, 2 );
function so_26429482_add_recipient( $email, $order ) {
// !empty($order) prevents a fatal error in WooCommerce Settings
// !empty($email) prevents the duplicate email from being sent in cases where the filter is run outside of when emails are normally sent. In my case, when using https://wordpress.org/plugins/woo-preview-emails/
if(!empty($order) && !empty($email)) {
$additional_email = "isf#domain.co";
if( $order->get_payment_method() == "cod" ) {
$email .= ',' . $additional_email;
} else {
$email .= ',another#domain.co';
}
}
return $email;
}
Each email has a filter that allows you to adjust the recipients of that email. The filter name is essentially woocommerce_email_recipient_{$email_id}.
So the following would filter the "to" addresses for the "new_order" email.
add_filter( 'new_order' , 'so_26429482_add_recipient', 20, 2 );
function so_26429482_add_recipient( $email, $order ) {
$additional_email = "somebody#somewhere.net";
if( $order->shipping_postcode == "90210" ){
$email = explode( ',', $email );
array_push( $email, $additional_email );
}
return $email;
}
I'm not 100% certain on the conditional logic, but I think that should check the shipping zip code and subsequently send to the additional email address.