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.
Related
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.
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.
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
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.
I'm trying to add the product inventory value to the low inventory level notification emails in Woocommerce.
The email notification code is on line 386 of WC_Emails class, however, it doesn't have an action so I'm not sure of the best way to add it without changing the core file.
This mail is just a function attached to a hook. You should be able to remove it and then add your own. 100% untested, so use at your own risk.
I'm not 100% sure on how to remove the callback from the woocommerce_low_stock_notification because I'm not certain how to access the WC_Emails instance.
function so_27786112_remove_notification(){
$emails = WC_Emails::instance();
remove_action( 'woocommerce_low_stock_notification', array( $emails, 'low_stock' ) );
}
add_action( 'woocommerce_init', 'so_27786112_remove_notification' );
But as Andrew suggested you can just duplicate the low_stock() callback and modify it as you'd like. I've simply added a custom function to the appropriate hook. I've really only changed the $message equation to use the get_stock_quantity() method.
function so_27786112_stock_notification( $product ) {
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$subject = apply_filters( 'woocommerce_email_subject_low_stock', sprintf( '[%s] %s', $blogname, __( 'Product low in stock', 'woocommerce' ) ), $product );
$sku = ($product->sku) ? '(' . $product->sku . ') ' : '';
if ( ! empty( $product->variation_id ) )
$title = sprintf(__( 'Variation #%s of %s', 'woocommerce' ), $product->variation_id, get_the_title($product->id)) . ' ' . $sku;
else
$title = sprintf(__( 'Product #%s - %s', 'woocommerce' ), $product->id, get_the_title($product->id)) . ' ' . $sku;
$qty = $product->get_stock_quantity();
$message = $title . sprintf( __( ' has %s remaining in stock.', $qty ) );
// CC, BCC, additional headers
$headers = apply_filters('woocommerce_email_headers', '', 'low_stock', $product);
// Attachments
$attachments = apply_filters('woocommerce_email_attachments', array(), 'low_stock', $product);
// Send the mail
wp_mail( get_option('woocommerce_stock_email_recipient'), $subject, $message, $headers, $attachments );
}
add_action( 'woocommerce_low_stock_notification', 'so_27786112_stock_notification' );
If the first part didn't work, you should get 2 emails.
One option would be to make your own class that extends the WC_Emails class. Then copy the low_stock() method from their class into yours, which will override their function with yours. That way you can adjust your own function however you want and you won't be editing the core file.
Another option would be to add the filters you would need there and submit a pull request back to WooThemes. They may just accept it and make it part of the core.