Send same Email to extra recipient as the admin received - php

On adding extra recipient on woocommerce order, the recipient generally receives the mail same as the customer receives.But i want to send extra recipient the mail what an admin receives.
Below is my working code which receives email in customer format
add_filter(
'woocommerce_email_recipient_customer_processing_order',
'shop_email_recipient_filter_function',
10,
2
);
add_filter(
'woocommerce_email_recipient_customer_completed_order',
'shop_email_recipient_filter_function',
10,
2
);
function shop_email_recipient_filter_function($recipient, $order) {
$shop = get_post_meta( $order->id, '_restaurant_id', true );
$shop_owner_email = get_the_author_meta(
'user_email',
get_post_field('post_author', $shop)
);
$recipient .= ', ' . $shop_owner_email;
return $recipient;
}
i want to send admin mail format

Related

How can I send on-hold WooCommerce email notification to admin as well as customer

By default, WooCommerce only sends On-Hold order notifications to customers (not sure why, as it seems having a shop manager know when an order is on-hold would be pretty important...)
I have tried implementing the following snippets into my child theme's functions.php based on the threads I have found researching here and on Google: Send on-hold order status email notification to admin
add_filter( 'woocommerce_email_headers', 'custom_admin_email_notification', 10, 3);
function custom_admin_email_notification( $headers, $email_id, $order ) {
if( 'customer_on-hold_order' == $email_id ){
// Set HERE the Admin email
$headers .= 'Bcc: My name <my#email.com>\r\n';
}
return $headers;
}
I have also tried the following snippet from the same thread:
// Send on-hold order status email notification to admin
add_filter( 'woocommerce_email_headers', 'custom_admin_email_notification', 10, 3);
function custom_admin_email_notification( $headers, $email_id, $order ) {
if( strpos($email_id,'hold') > 0 ){
$headers .= 'Bcc: Admin User <admin#example.com>'. "\r\n";
}
return $headers;
}
None of these seem to work when an order is changed from Pending Payment to On-Hold. Yes, I have changed the email in the snippets to my admin email. I am looking for a solution where an admin will be BCC'd on the Customer On-Hold email either when this happens automatically (from pending payment to on-hold) or manually when editing an order.
Adding utf8_decode might help
Replace customer_on-hold_order with customer_on_hold_order
Tested with Wordpress 5.8.1 and WooCommerce 5.6.0
So you get:
function filter_woocommerce_email_headers( $header, $email_id, $order ) {
// Compare
if ( $email_id == 'customer_on_hold_order' ) {
// Prepare the the data
$formatted_email = utf8_decode( 'My test <my_test#email.com>' );
// Add Bcc to headers
$header .= 'Bcc: ' . $formatted_email . '\r\n';
}
return $header;
}
add_filter( 'woocommerce_email_headers', 'filter_woocommerce_email_headers', 10, 3 );

Set admin email as BCC for WooCommerce cancelled and failed orders

I am currently using this code to send out notifications on failed and cancelled orders.
function wc_cancelled_order_add_customer_email( $recipient, $order )
{
return $recipient . ',' . $order->billing_email;
}
add_filter( 'woocommerce_email_recipient_cancelled_order', 'wc_cancelled_order_add_customer_email', 10, 2 );
add_filter( 'woocommerce_email_recipient_failed_order', 'wc_cancelled_order_add_customer_email', 10, 2 );
The problem is that those two admin emails I have set in the system under woocommerce settings are also added to the email recipient together with the customers email.
Is it possible to tweak this, so the admin E-mail adresses are on BCC instead, so the customer can't see their email addresses?
You need to make some little changes to your hooked function and to add an additional hooked function to handle the admin email as BCC recipient:
add_filter( 'woocommerce_email_recipient_cancelled_order', 'custom_cancelled_and_failed_order_email_recipients', 10, 2 );
add_filter( 'woocommerce_email_recipient_failed_order', 'custom_cancelled_and_failed_order_email_recipients', 10, 2 );
function custom_cancelled_and_failed_order_email_recipients( $recipient, $order ) {
// Check that the WC_Order object always exist
if( is_a( $order, 'WC_Order' ) )
$recipients = $order->get_billing_email();
return $recipients;
}
add_filter( 'woocommerce_email_headers', 'custom_cancelled_and_failed_order_email_headers', 20, 3 );
function custom_cancelled_and_failed_order_email_headers( $header, $email_id, $order ) {
// Only for 'cancelled' and 'failed' order notifications
if( in_array( $email_id, ['cancelled_order', 'failed_order'] ) ) {
// Get original admin recipient
$recipient = WC()->mailer()->get_emails()['WC_Email_Cancelled_Order']->settings['recipient'];
// Add Admin email As Bcc recipient
$header .= 'Bcc: ' . $recipient . "\r\n";
}
return $header;
}
Code goes in functions.php file of your active child theme (or active theme). It should work.

Add a reply email to customer notifications based on shipping methods in Woocommerce

All order email sending to CUSTOMER from default woocommerce email address, for example, webshop#shop.com and this is OK, but I want add a reply email address for these emails, so user able to reply this custom address, such as reply#webshop.com.
Also I want set this reply email address based on $order shipping method. If shipping method is 'local_pickup_plus' set reply address to reply1#webshop.com, else set reply2#webshop.com.
I figure out, I can modify headers with woocommerce_email_headers filter, but only for email sent to the admin.
add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 3);
function mycustom_headers_filter_function( $headers, $object, $order ) {
if ($object == 'new_order') {
$headers .= 'Reply-to: teszt#teszt.hu';
}
return $headers;
}
How can I set this for customer emails?
The following code will allow you to add a different reply email conditionally based on shipping methods for customer email notifications:
// Utility function to get the shipping method Id from order object
function wc_get_shipping_method_id( $order ){
foreach ( $order->get_shipping_methods() as $shipping_method ) {
return $shipping_method->get_method_id();
}
}
// Add coditionally a "reply to" based on shipping methods IDs for specific email notifications
add_filter( 'woocommerce_email_headers', 'add_headers_replay_to_conditionally', 10, 3 );
function add_headers_replay_to_conditionally( $headers, $email_id, $order ) {
// Avoiding errors
if ( ! is_a( $order, 'WC_Order' ) || ! isset( $email_id ) )
return $headers;
// The defined emails notifications to customer
$allowed_email_ids = array('customer_on_hold_order', 'customer_processing_order', 'customer_completed_order');
// Only for specific email notifications to the customer
if( in_array( $email_id, $allowed_email_ids ) ) {
// Local Pickup Plus shipping method
if( wc_get_shipping_method_id( $order ) === 'local_pickup_plus' ){
$headers .= "Reply-to: reply1#webshop.com". "\r\n"; // Email adress 1
}
// Other shipping methods
else {
$headers .= "Reply-to: reply2#webshop.com". "\r\n"; // Email adress 2
}
}
return $headers;
}
Code goes in function.php file of your active child theme (active theme). Tested and works.

Change email subject for custom order statuses in Woocommerce 3

I have successfully changed email subject for Woocommerce processing order (using this thread):
add_filter( 'woocommerce_email_subject_customer_processing_order', 'email_subject_procs_order', 10, 2 );
function email_subject_procs_order( $formated_subject, $order ){
return sprintf( esc_html__( 'Example of subject #%s', 'textdomain'), $order->get_id() );
}
But I want send processing order email again with new subject after order status is changed, so I followed this tread to tweak subject etc.
add_action('woocommerce_order_status_order-accepted', 'backorder_status_custom_notification', 20, 2);
function backorder_status_custom_notification( $order_id, $order ) {
// HERE below your settings
$heading = __('Your Awaiting delivery order','woocommerce');
$subject = sprintf( esc_html__( 'New subject #%s', 'textdomain'), $order->get_id() );
// Getting all WC_emails objects
$mailer = WC()->mailer()->get_emails();
// Customizing Heading and subject In the WC_email processing Order object
$mailer['WC_Email_Customer_Processing_Order']->heading = $heading;
$mailer['WC_Email_Customer_Processing_Order']->settings['heading'] = $heading;
$mailer['WC_Email_Customer_Processing_Order']->subject = $subject;
$mailer['WC_Email_Customer_Processing_Order']->settings['subject'] = $subject;
// Sending the customized email
$mailer['WC_Email_Customer_Processing_Order']->trigger( $order_id );
}
But only first email subject change is accepted. Is there way to get it work together?
Is if( $order->has_status( 'order-accepted' )) right to be used?
You need to use your custom status in a IF statement to avoid that problem, this way:
add_filter( 'woocommerce_email_subject_customer_processing_order', 'email_subject_procs_order', 10, 2 );
function email_subject_procs_order( $formated_subject, $order ){
// We exit for 'order-accepted' custom order status
if( $order->has_status('order-accepted') )
return $formated_subject;
return sprintf( esc_html__( 'Example of subject #%s', 'textdomain'), $order->get_id() );
}
Code goes in function.php file of your active child theme (or active theme). It should works.

Output HTML using existing PHP in wordpress functions.php

I am using the below to send an email to a user each time a custom field within their profile is updated. I can output plain text into the email body using the $message line which is great. How can I adapt this so I can output html where the $message goes?
// IF CUSTOM FIELD CHANGES
function sr_user_profile_update_virtuosity( $user_id, $old_user_data ) {
$old_user_data = get_transient( 'sr_old_user_data_' . $user_id );
$user = get_userdata( $user_id );
if($old_user_data->virtuosity != $user->virtuosity) {
$admin_email = $user->user_email;
$message = sprintf( __( 'I want to output HTML here' ) ) . "\r\n\r\n";
wp_mail( $admin_email, sprintf( __( 'IMPORTANT: Your newly purchased product is ready for you' ), get_option('blogname') ), $message );
}
}
add_action( 'profile_update', 'sr_user_profile_update_virtuosity', 10, 2 );
You need to set the content type to html, by default its text/plain.
You can do this by using wp_mail_content_type filter.
add_filter( 'wp_mail_content_type', 'set_content_type' );
function set_content_type( $content_type ) {
return 'text/html';
}
For more info see here.

Categories