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
Related
I'm looking for a php snippet that I can add in order to add a placeholder to WooCommerce "New Order" emails. I want to add custom user meta as the placeholder.
As far as I can tell, I'd need to add this to functions.php.
function wpa_filter_email_format_string( $string, $email ) {
// Get WC_Order object from email
$order = $email->object;
$wpcf_sales_rep = get_user_meta($user->ID, 'wpcf_sales_rep', true);
// Add new placeholders
$new_placeholders = array(
'{_sales_rep}' => $order->get_user_meta($user->ID, 'wpcf_sales_rep', true),
);
// return the string with new placeholder replacements
return str_replace( array_keys( $new_placeholders ), array_values( $new_placeholders ), $string );
}
add_filter( 'woocommerce_email_format_string' , 'wpa_filter_email_format_string', 20, 2 );
Not sure if I need the 4th line - so any help or guidance on correcting this code would be perfect, thanks!
I think you want to know about this line.
$wpcf_sales_rep = get_user_meta($user->ID, 'wpcf_sales_rep', true);
It doesn't do anything useful: it stores your usermeta data into a variable called $wpcf_sales_rep, but then the rest of your code doesn't use the variable anywhere.
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've tried to replace username with first name billing using the code below changed from this answer thread, but keep getting 500 error.
If I use first and last name it works but I would prefer to use first name only.
Code is as follows:
add_filter( 'woocommerce_new_customer_data', 'custom_new_customer_data', 10, 1 );
function custom_new_customer_data( $new_customer_data ){
// Complete HERE in this array the wrong usernames you want to replace (coma separated strings)
$wrong_user_names = array( 'info', 'contact' );
// get the first billing name
if(isset($_POST['billing_first_name'])) $first_name = $_POST['billing_first_name'];
if( ( ! empty($first_name) ) ) && in_array( $new_customer_data['user_login'], $wrong_user_names ) ){
// the customer billing complete name
$first_name = $first_name;
// Replacing 'user_login' in the user data array, before data is inserted
$new_customer_data['user_login'] = sanitize_user( str_replace( $first_name ) );
}
return $new_customer_data;
}
My question would be, how would I configure WooCommerce to generate the username by the custom fields: First Name (billing_first_name) instead of full name or username?
Try the following, to replace username by the billing firstname during checkout registration:
add_filter( 'woocommerce_new_customer_data', 'customer_username_based_on_firstname', 20, 1 );
function customer_username_based_on_firstname( $new_customer_data ){
// Complete HERE in this array the wrong usernames you want to replace (coma separated strings)
$wrong_user_names = array( 'info', 'contact' );
// get the first billing name
if(isset($_POST['billing_first_name'])) $first_name = $_POST['billing_first_name'];
if( ! empty($first_name) && ! in_array( $_POST['billing_first_name'], $wrong_user_names ) ){
// Replacing 'user_login' in the user data array, before data is inserted
$new_customer_data['user_login'] = sanitize_user( $first_name );
}
return $new_customer_data;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Your error was coming from str_replace( $first_name ). This php function needs 3 arguments.
I want display the external table value in WooCommerce checkout from company name field. We can create external table and insert some of company name.
When user signup our service in checkout field, customer should select the company name in a custom select options field. This values should be displayed from a custom database table.
How can I achieve this?
Thanks in Advance.
We are going to first unset the field type of company, and then to change it to a type 'select' (selector). Then we will maque a query to get all options key/values from your custom database table (Let's say is called 'wp_companies'):
(ONLY FOR TESTING) NO database query here (with 3 options for companies):
add_filter( 'woocommerce_default_address_fields' , 'set_custom_company_checkout_field' );
function set_custom_company_checkout_field( $address_fields ) {
unset($fields['company']['type']);
$address_fields['company']['type'] = 'select';
$address_fields['company']['options'] = array(
'option_1' => 'Company 1',
'option_2' => 'Company 2',
'option_3' => 'Company 3'
);
// (optional)
// $address_fields['company']['default'] = 'Company 1';
return $address_fields;
}
Paste this code in function.php file located in your active child theme (or theme).
This code is tested and works…
THE REAL CODE: Making the query from database custom table (to be adapted):
add_filter( 'woocommerce_default_address_fields' , 'set_custom_company_checkout_field' );
function set_custom_company_checkout_field( $address_fields ) {
// Unset company field type
unset($fields['company']['type']);
global $wpdb;
$select_options = array();
// ### you will need to replace names table and columns and adapt this !!!
$query = "SELECT id, company FROM table";
$companies_name = $wpdb->get_results($query);
// Storing object $company_name keys/values in $select_options array
foreach ( $companies_name as $company_name )
{
$key = 'option_'. $company_name->ID;
$value = $company_name->custom_column_value;
$select_options[$key] = $value ;
}
$address_fields['company']['type'] = 'select';
$address_fields['company']['options'] = $select_options;
// (optional)
// $address_fields['company']['default'] = $select_options['option_1'];
return $address_fields;
}
Paste this code in function.php file located in your active child theme (or theme).
References:
WooThemes - Customizing checkout fields using actions and filters
WooCommerce - Overriding billing state and post code on existing checkout fields
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.