Woocommerce - Send email to other people [duplicate] - woocommerce

This question already has answers here:
Customizing email headers in WooCommerce email notifications
(1 answer)
How to add Custom Field as a CC to WooCommerce Customer Order Email
(1 answer)
Woocommerce: Adding second email address not working, unless recipient is admin
(2 answers)
Closed 3 years ago.
I have a custom field that contains an additional customer email account.
My idea is that when an order changes state WAIT, PENDING, PROCESSING or COMPLETED, it will reach the email of the one configured in this field.
Is this possible?
There is no problem that requires programming, but I don't know what hook to use.
Thank you.

Add the follows code snippet in your active theme's functions.php -
function add_cc_to_wc_order_emails( $header, $mail_id, $mail_obj ) {
$available_for = array( 'customer_on_hold_order', 'customer_processing_order', 'customer_completed_order' );
if( in_array( $mail_id, $available_for ) ) {
$cc_email = 'addyour#ccmail.com';
$cc_username = "yourCCUser";
$formatted_email = utf8_decode( $cc_username . ' <' . $cc_email . '>' );
$header .= 'Cc: ' . $formatted_email . "\r\n";
}
return $header;
}
add_filter( 'woocommerce_email_headers', 'add_cc_to_wc_order_emails', 99, 3 );

This might be quite useful:
https://www.skyverge.com/blog/add-woocommerce-email-recipients-conditionally/
I would take a look at the Considerations section.

You could use woocommerce_order_status_changed hook for example to notify someone each time an order change of status, like in this example:
add_action('woocommerce_order_status_changed', 'send_custom_email_notifications', 10, 4 );
function send_custom_email_notifications( $order_id, $old_status, $new_status, $order ){
$to_email = 'john.doe#gmail.com'; // <= Replace with your email custom field (the recipient)
$shop_name = __('Shop name'); // Set the shop name
$admin_email = 'shop#email.com'; // Set default admin email
$subject = sprintf( __('Order %s has changed to "%s" status'), $order_id, $new_status ); // The subject
$message = sprintf( __('Order %s has changed to "%s" status'), $order_id, $new_status ); // Message content
$headers = sprintf( __('From: %s <%s>'), $shop_name, $admin_email ) . "\r\n"; // From admin email
wp_mail( $to_email, $subject, $message, $headers ); // Send the email
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works

Related

Add a Supplier custom field email address as BCC to woocommerce email New Order

I have a holiday letting wordpress site which has the advanced custom field "Supplier Email" in the add listing page. When a rental is booked the Supplier Email needs to be added to the woocommerce New Order email under BCC, i have tried numerous options but they have not worked, the Supplier Email is in the post meta so it needs to be pulled in to the new order email. This is the code i have in my functions.php file:
add_filter( 'woocommerce_email_headers', 'bcc_to_email_headers', 10, 3 );
function bcc_to_email_headers( $headers, $email_id, $order ) {
if ( $email_id === 'new_order' ) {
$supplier_email = get_field( 'supplier_email_main',$post_id);
if ( $supplier_email ) {
$headers .= "CC: Supplier <" . $supplier_email . ">\r\n";
$headers .= "BCC: New Order <myemail#gmail.com>" . "\r\n";
}
}
return $headers;
}
The gmail address(this would go to my personal email, have hidden it here for obvious reasons) i added to BCC didnt even get sent. Not sure how to proceed with this, any help would be greatly appreciated, please note i am not a wordpress developer. Thanks in advance.
try this
add_filter( 'woocommerce_email_headers', 'add_bcc_header', 10, 3 );
function add_bcc_header( $headers, $id, $order ) {
if ( $id == 'new_order' ) {
$headers .= 'Bcc: gmailaddress#gmail.com' . "\r\n";
}
return $headers;
}

Add product link to out of stock email notification in WooCommerce

I need to get the product link into out of stock emails sent to admin.
This code does not have the desired result even though I am, as far as I know, using the right filter hook? Any advice?
add_filter('woocommerce_email_content_no_stock', 'add_product_url_to_out_of_stock_email', 10, 2);
function add_product_url_to_out_of_stock_email( $message, $product ) {
global $product; // with or without this is the same result
return $message ."<br>\n". get_edit_post_link( $product->get_id() );
}
You can add/use WC_Product::get_permalink() – Product permalink to customize $message to suit your needs.
So you get:
function filter_woocommerce_email_content_no_stock ( $message, $product ) {
// Edit message
$message = sprintf( __( '%s is out of stock.', 'woocommerce' ), '' . html_entity_decode( wp_strip_all_tags( $product->get_formatted_name() ), ENT_QUOTES, get_bloginfo( 'charset' ) ) . '' );
return $message;
}
add_filter( 'woocommerce_email_content_no_stock', 'filter_woocommerce_email_content_no_stock', 10, 2 );
IMPORTANT: This answer will not work by default because wp_mail() is used as mail function, where the content type is text/plain which does not allow using HTML
So to send HTML formatted emails with WordPress wp_mail(), add this extra piece of code
function filter_wp_mail_content_type() {
return "text/html";
}
add_filter( 'wp_mail_content_type', 'filter_wp_mail_content_type', 10, 0 );
Related: Adding product hyperlink to low stock notification email in WooCommerce

How to check WooCommerce order meta before sending email notification

I'm trying to add custom meta to my new order emails. At this moment, custom meta shows up when I use the "Resend new order notification", but it doesn't when a purchase is made through the website.
I suspect this has to do with my code not being able to check the Order Details before sending the email notification. Here's the code I came up with so far..
// Ref: https://www.businessbloomer.com/woocommerce-add-extra-content-order-email/
function custom_order_type_number_email( $order, $sent_to_admin, $plain_text, $email ) {
// Get custom Order Type and Order Number meta keys
$custom_order_type = get_post_meta( $order->get_id(), 'custom_order_type', true );
$custom_order_number = get_post_meta( $order->get_id(), 'custom_order_number', true );
// Add for New Order email notification only
if ( $email->id == 'new_order' ) {
if ( !empty( $custom_order_type ) ) {
echo '<p><b>Order type :</b> ' . $custom_order_type . '</p>';
} else {
echo '<p><b>Order type :</b> Website</p>';
}
if ( !empty( $custom_order_number ) ) {
echo '<p><b>Order no :</b> ' . $custom_order_number . '</p>';
} else {
echo '<p><b>Order no :</b> New</p>';
}
}
}
add_action( 'woocommerce_email_after_order_table', 'custom_order_type_number_email', 20, 4 );
What I'm trying to do ideally is..
When the customer purchases from the website, this new order will be without the custom meta. So the custom meta in the email will show Order type: Website / Order number: New.
And if the admin manually adds a new order with defined custom meta, this new order email notification would get and show the saved custom meta later.
I hope this made sense. (>_<) And thanks in advance for the help.

Woocommerce Get Vendor Information with Product ID

I am using Woo Commerce with WC Vendor and WC Booking plugin. I want to send booking notification to vendor. Currently it sends notification to Customer and Administrator and when admin changes product status to processing & completed, then it sends notification to vendor. However, I want to send vendor notification along with admin notification.
I tried this hook:
add_action( 'woocommerce_new_booking', 'new_order_email_to_vendor', 10, 4 );
function new_order_email_to_vendor( $order ){
$emails = WC()->mailer()->get_emails();
if ( ! empty( $emails ) ) {
$emails['WC_Product_Vendors_Order_Email_To_Vendor']->trigger( $order );
}
}
But it throws an error:
Fatal error: Call to a member function get_date_created() on boolean in /wp-content/plugins/woocommerce-product-vendors/includes/emails/class-wc-product-vendors-order-email-to-vendor.php on line 56
So, now I am trying to hook vendor email directly along with customer and administrator email in this line:
$this->recipient = $this->get_option( 'recipient', get_option( 'admin_email' ), 'WILL ADD VENDOR EMAIL HERE' );
in file:
wp-content/plugins/woocommerce-bookings/includes/emails/class-wc-email-new-booking.php
At this stage, I have bookable product id available and I am trying to pull vendor information using product id but I don't found any information.
I tried:
get_post()
get_post_meta()
get_post_meta_by_id()
The Question is: How to get vendor information (specifically email) using product id?
I don't have and I have never used WC Vendors plugin as this is a non official commercial plugin (not made by automatic).
To get the vendor ID (after searching a bit) you can get it this way from a product ID:
$vendor_id = get_post_field( 'post_author', $product_id );
Now you can get the vendor email this way:
$vendor_id = get_post_field( 'post_author', $product_id );
$vendor = get_userdata( $vendor_id );
$email = $vendor->user_email;
May be a good turn around:
You can use the dedicated filter hook woocommerce_email_recipient_{$this->id} where $this->id is the ID of the notification type, for new_booking email ID (and also for testing new_order email ID too).
This will allow you to add additional email recipients.
In email notifications hooks, the Order object is nearly always defined. As In an order you can have many items (different products rom different vendors), you will need to get the vendor ID from each.
In the code below I add to the recipients the vendors emails for new_booking and new_order email notifications:
add_filter( 'woocommerce_email_recipient_new_booking', 'additional_customer_email_recipient', 10, 2 );
add_filter( 'woocommerce_email_recipient_new_order', 'additional_customer_email_recipient', 10, 2 ); // Optional (testing)
function additional_customer_email_recipient( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
$additional_recipients = array(); // Initializing…
// Iterating though each order item
foreach( $order->get_items() as $item_id => $line_item ){
// Get the vendor ID
$vendor_id = get_post_field( 'post_author', $line_item->get_product_id());
$vendor = get_userdata( $vendor_id );
$email = $vendor->user_email;
// Avoiding duplicates (if many items with many emails)
// or an existing email in the recipient
if( ! in_array( $email, $additional_recipients ) && strpos( $recipient, $email ) === false )
$additional_recipients[] = $email;
}
// Convert the array in a coma separated string
$additional_recipients = implode( ',', $additional_recipients);
// If an additional recipient exist, we add it
if( count($additional_recipients) > 0 )
$recipient .= ','.$additional_recipients;
return $recipient;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This should work without errors.

Woocommerce - Bcc email only when the admin is recipient OR by Order Status

How to Get email recipient to make an conditional function to add extra bcc email header on email only to admin OR by Order Status?
add_filter( 'woocommerce_email_headers', 'profit_extra_email', 10, 2);
function profit_extra_email( $headers ) {
$headers .= 'BCC: Vendas Promocional Fitness <eu#site.com>' . "\r\n";
return $headers;
}
This function above work, but bcc email retrieve all emails. I need retrieve only emails to admin OR by Order Status.
Help any!! :O
There are 3 variables passed to the woocommerce_email_headers filter. Since none of them appear to be the $in_admin variable that is passed to templates, we have to take a different approach. The second param is $email_id which is a unique identifier for each email class. Therefore we can make an array of all the admin emails and check if the current email is in that array. If so, add your CC.
function so_31737121_email_headers( $headers, $email_id, $order ){
$admin_emails = array(
'low_stock',
'no_stock',
'backorder',
'cancelled_order',
'new_order'
);
if( in_array( $email_id, $admin_emails ) ){
$headers .= 'BCC: Vendas Promocional Fitness <eu#site.com>' . "\r\n";
}
return $headers;
}
add_action( 'woocommerce_email_headers', 'so_31737121_email_headers', 10, 3 );

Resources