I want to send e-mails to user account email and not to billing email in WooCommerce.
I only found that code but I think this is outdated and I also don't want to send 2 mails only one to customer user email.
Code here:
https://www.webdesign101.net/send-e-mails-user-account-email-billing-email-woocommerce/
Tried this code but outdated and also 2 mails instead of one going to user email.
And I didn't found the line in woocommerce/templates/email to change the header from billing_email to user_email.
I want so send emails from WooCommerce to user email not to billing email.
function wc_change_new_order_email_recipient($recipient, $order) {
global $woocommerce;
$user = $order->get_user();
$recipient[] = $user->user_email;
return $recipient;
}
add_filter('woocommerce_email_recipient_new_order', 'wc_change_new_order_email_recipient', 10, 2);
Related
I'm faced with a little problem.
I need to send the Customer Invoice / Order Details email automatically when an order is completed.
I haven't found any action or filter to apply in this case an the internet is not useful. Has anyone an idea how to send this email when the order status switches?
Best regards!
You can use woocommerce_order_status_completed hook and WC_Email_Customer_Invoice
add_action( 'woocommerce_order_status_completed', 'mysite_woocommerce_order_status_completed', 10, 1);
function mysite_woocommerce_order_status_completed( $order_id ) {
// Send invoice email to customer
//use order ID as trigger value
WC()->mailer()->emails['WC_Email_Customer_Invoice']->trigger($order_id);
}
I have a very specific usecase where I want to send an email to the admin of the site whenever a new user is registered . This email is not a welcome email to user but an email to admin which contains details of new registered user so that I can contact my users later without having to referring to database .
Can I achieve this in wordpress . Thankyou. I am new to wordpress .
In wp-includes/pluggable.php there is a function "wp_new_user_notification" which handles the new registration emails that are being sent to user. Prior to 4.3 it also had an option to send a copy to admin which is deprecated now. In that part of code just take code of wp_email from user block and change the "to" parameters for admin.
It will keep sending it to users and will also send a copy to admin.
You can use the WP action hook ‘user_register’ which fires right after a new user has registered and passes the id of the new user to the callback function.
add_action( 'user_register', 'inform_admin');
function inform_admin ($user_id) {
$user = get_userdata($user_id);
//$user is now an object containing details of the user. e.g. $user->first_name #see https://developer.wordpress.org/reference/functions/get_userdata/ for more details
$email_context = ““;
$subject = ““;
// get the admin email
$admin_email = get_options('admin_email');
// send the email
wp_mail( $admin_email, $subject, $email_context);
}
I need WooCommerce to send specific emails from the customer's email address rather than the admin email address.
Background:
I'm setting up a paid support service which uses WooCommerce for payments and bbPress as a ticketing system.
A user orders a service product through WooCommerce and a new WooCommerce order is created. The order needs to then populate a ticketing system and my workaround is to have the 'New order' email sent to an email inbox and then pulled in by the ticketing system. Obviously this is not perfect and introduces a point of failure but it's fine for now.
The problem is that the new order email is sent from the WooCommerce admin email address, so the new ticket is associated with that account rather than the account of the user who created the order.
So I need the email from address to be the address of the user that submitted the order.
Is this possible with a hook?
in woocommerce/includes/class-wc-emails.php we have the following:
public function send( $to, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments = '' ) {
// Send.
$email = new WC_Email();
return $email->send( $to, $subject, $message, $headers, $attachments );
}
This doesn't include the sender, and as it's buried in a class, could it be overridden anyway?
Greatful for any pointers.
(PS I do appreciate there are issues with this approach, including emails being flagged as spam, but initially this will be manageable until I get a more robust system in place)
(PPS I'm aware there is an almost identical question but the links aren't working and relates to a very out of date WooCommerce)
That's impossible to send the email from your customer email address. You can only set the customer email to Reply-To in WP_Mail header.
In my experience, you should send another email to your sepcific email inbox, and skip the email sent by WooCommerce by default. Use this hook:
add_action('woocommerce_order_status_pending_to_processing', 'order_created_send_email');
function order_created_send_email($order_id, $order) {
$customer_email = $order->get_billing_email();
/* Code to send email with Reply-To: customer email */
}
I'm looking for a solution where we can disable woocommerce sending email notification when an order is done by a client from a specific customer group (user role).
I found answer about a situation what prevent sending email for a specific product-ID.
Disable WooCommerce email notification for specific product.
Maybe this could be possible for our 'problem' too?
Kind regards,
Kees
You can use the hook for any email and inside the callback function you can check if the user has a specific role
function change_new_order_email_recipient( $recipient, $order ) {
global $woocommerce;
$uid = $order->get_user_id();
$user_meta=get_userdata($uid);
$user_roles=$user_meta->roles;
if(in_array('customer', $user_roles)){ // Prevent email if user role is customer
$recipient ='';
}
return $recipient;
}
add_filter('woocommerce_email_recipient_customer_completed_order', 'change_new_order_email_recipient', 1, 2);
I have checked the code rapidly and is working
WooCommerce -> Settings -> EmailS -> the first two options, "FROM: Name, FROM: Email", are the Sender's email and name. When an order is placed a notification email is sent to both Shop Manager and Customer from the same Sender's email and name (which we set from admin dashboard).
When Customer replies to that email, he basically replies to Shop Manager and it works fine.
But Shop Manager receives the notification email from his (given) email address, in actually there would be client's email.
P.S: I want that Shop Manager gets the email from the customer's billing email and customer gets it from Shop Manager's given email.
https://wordpress.org/plugins/woocommerce/
The only way I can think of to achieve this is to programmatically generate the Shop Manager notification email using one of the system action hooks. Since there is only one field in the "from" settings, it will be used (by default) for all the outbound messages.
That said, check out the Code Snippets plugin at https://wordpress.org/plugins/code-snippets/. You can put some code here (and it will be saved to the DB, and included the system code at runtime) that could generate this programmatic email when, say, a new order is generated.
Hope it helps.
After going through your question it made sense that for a shop owner it needs to be like the mail should have sent from address as the client's email address, so that he can reply to each order in a single email thread.
//filter for the "mail from" email address which is set in WooCommerce Settings Email Tab-> Email Sender Options -> "From" Email Address which is generally set to the shop admin email address or a support team email address.
add_filter('wp_mail_from', 'wdm_sent_from_email', 99, 1);
//function which will change the mail from email address to the the Customer's billing address if the mail this filter is running which sending mail to the admin.
function wdm_sent_from_email( $sent_from = '' ) {
//check whether our custom parameter is set or not.
if ( isset($_POST['wdm_sent_to_admin']) ) {
//check whether the mail is sent to the admin and other recieptent other than customer
if ( $_POST['wdm_sent_to_admin'] ) {
//set it to the customer's billing email
$sent_from = $_POST['billing_email'];
//set this parameter back to false
$_POST['wdm_sent_to_admin'] == false;
}
}
return $sent_from;
}
//filter for email from name
add_filter('wp_mail_from_name', 'wdm_sent_from_name', 99, 1);
function wdm_sent_from_name( $sent_from_name = '' ) {
//check whether our custom parameter is set or not.
if ( isset($_POST['wdm_sent_to_admin_from_name']) ) {
//check whether the mail is sent to the admin and other recieptent other than customer
if ( $_POST['wdm_sent_to_admin_from_name'] ) {
//set sent mail from name eg. "Website-Name customer"
$sent_from_name = wp_specialchars_decode(esc_html(get_option('woocommerce_email_from_name')), ENT_QUOTES) . " customer";
//set this parameter back to false
$_POST['wdm_sent_to_admin_from_name'] = false;
}
}
return $sent_from_name;
}
//action were we will set and the parameter to indicate whether the mail is sent to admin or customers.
add_action('woocommerce_email_header', 'wdm_header_function', 99, 1);
function wdm_header_function( $email_heading ) {
if ( $email_heading == 'New customer order' ) {
//parameter to indicate whether to change the from email in the mail.
$_POST['wdm_sent_to_admin'] = true;
//parameter to indicate whether to change the from name in the mail.
$_POST['wdm_sent_to_admin_from_name'] = true;
//Just to indicate in mail sent to admin that the sent from email is set in code.
echo "<b>Its Because you have chosen to have this email sent from Customer's Email id.</b>";
} else {
//parameter to indicate whether to change the from email in the mail.
$_POST['wdm_sent_to_admin'] = false;
//parameter to indicate whether to change the from name in the mail.
$_POST['wdm_sent_to_admin_from_name'] = false;
}
}
Note : This might give you and alert message while viewing the message at your side only saying "This message may not have been sent by: (your customer's billing address) " as this is not really sent by a customer to you.
Try out the above code and lemme know whether it works for you and accomplishes your purpose.
I think you can also use wordpress hooks for that:
// Function to change email address
function wpb_sender_email( $original_email_address ) {
return 'tim.smith#example.com';
}
// Function to change sender name
function wpb_sender_name( $original_email_from ) {
return 'Tim Smith';
}
// Hooking up our functions to WordPress filters
add_filter( 'wp_mail_from', 'wpb_sender_email' );
add_filter( 'wp_mail_from_name', 'wpb_sender_name' );