I'm trying to hook into 'woocommerce_email_recipient_booking_confirmed' to update/add to the recipient field. The link below works perfectly for default WooCommerce emails (and their actions). The action works if I manually type in an email within the action but I need the email to come from the customer input (via a Custom Field on the product page).
I believe it is something to do with class-wc-email-booking-confirmed.php. When I compare it against class-wc-email-customer-completed-order.php
class-wc-email-booking-confirmed.php
public function trigger( $booking_id )
class-wc-email-customer-completed-order.php
public function trigger( $order_id, $order = false )
My code (which I haven't changed from the resource below):
add_filter( 'woocommerce_email_recipient_booking_confirmed', 'additional_customer_email_recipient', 9999, 2 ); // Completed Order
function additional_customer_email_recipient( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient; // when I comment this out, an ajax error is thrown. This is why I believe the order object directly accessible yet.
$additional_recipients = array(); // Initializing…
foreach( $order->get_items() as $item_id => $item_data ){
$email = wc_get_order_item_meta( $item_id, 'Email Address', true );
if( ! in_array( $email, $additional_recipients ) && strpos( $recipient, $email ) === false )
$additional_recipients[] = $email;
}
$additional_recipients = implode( ',', $additional_recipients);
if( count($additional_recipients) > 0)
{
$recipient = ','.$additional_recipients;
}
return $recipient;
}
Send Woocommerce Order to email address listed on product page
It was because it wasn't the order object, I just had to call get_order().
$order = $order->get_order();
Code:
add_filter( 'woocommerce_email_recipient_booking_confirmed', 'additional_customer_email_recipient', 10, 2 );
function additional_customer_email_recipient( $recipient, $order ) {
$order = $order->get_order();
$additional_recipients = array();
foreach( $order->get_items() as $item_id => $item_data ){
$email = wc_get_order_item_meta( $item_id, 'Email Address', true );
if( ! in_array( $email, $additional_recipients ) && strpos( $recipient, $email ) === false )
$additional_recipients[] = $email;
}
$additional_recipients = implode( ',', $additional_recipients);
if( count($additional_recipients) > 0)
{
$recipient = $additional_recipients;
}
return $recipient;
}
Related
I have this function which adds custom meta field to the product detail in all WooCommerce emails. But I need to show only after the order is paid (this can be also just the "completed" email).
add_action( 'woocommerce_order_item_meta_start', 'email_confirmation_display_order_items', 10, 3 );
function email_confirmation_display_order_items( $item_id, $item, $order ) {
// On email notifications for line items
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
$ot_address = get_post_meta( $item->get_product_id(), 'ot_address', true );
if ( ! empty($ot_address) ) {
printf( '<div>' . __("Terms: %s", "woocommerce") . '</div>', $ot_address );
}
}
}
I hoped that I can nest it inside if ( $email->id == 'customer_completed_order' ) {}, so the final code will look like this:
add_action( 'woocommerce_order_item_meta_start', 'email_confirmation_display_order_items', 10, 3 );
function email_confirmation_display_order_items( $item_id, $item, $order ) {
if ( $email->id == 'customer_completed_order' ) {
// On email notifications for line items
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
$ot_address = get_post_meta( $item->get_product_id(), 'ot_address', true );
if ( ! empty($ot_address) ) {
printf( '<div>' . __("Terms: %s", "woocommerce") . '</div>', $ot_address );
}
}
}
}
But it stops working after that change. Any advice?
As you can see in your code attempt, $email is not part of the woocommerce_order_item_meta_start hook. So to target certain WooCommerce email notifications, you will need a workaround.
Step 1) creating and adding a global variable, via another hook that only applies to WooCommerce email notifications.
// Setting global variable
function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
$GLOBALS['email_id'] = $email->id;
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 1, 4 );
Step 2) In the hook woocommerce_order_item_meta_start, use the global variable so we can target certain WooCommerce email notifications
function action_woocommerce_order_item_meta_start( $item_id, $item, $order, $plain_text ) {
// On email notifications for line items
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
// Getting the email ID global variable
$ref_name_globals_var = isset( $GLOBALS ) ? $GLOBALS : '';
$email_id = isset( $ref_name_globals_var['email_id'] ) ? $ref_name_globals_var['email_id'] : '';
// NOT empty and targeting specific email. Multiple statuses can be added, separated by a comma
if ( ! empty ( $email_id ) && in_array( $email_id, array( 'new_order', 'customer_completed_order' ) ) ) {
// Get meta
$ot_address = get_post_meta( $item->get_product_id(), 'ot_address', true );
// OR use to get meta
// $ot_address = $item->get_meta( 'ot_address' );
if ( ! empty( $ot_address ) ) {
printf( '<div>' . __( 'Terms: %s', 'woocommerce' ) . '</div>', $ot_address );
}
}
}
}
add_action( 'woocommerce_order_item_meta_start', 'action_woocommerce_order_item_meta_start', 10, 4 );
I've created my own email class in WooCommerce. Because I need a custom parameter in my email content, I've added a placeholder with this custom parameter to the wc email trigger function:
public function trigger( $order_id, $firstname, $lastname, $order = false ) {
$this->setup_locale();
if ( $order_id && ! is_a( $order, 'WC_Order' ) ) {
$order = wc_get_order( $order_id );
}
if ( is_a( $order, 'WC_Order' ) ) {
$this->object = $order;
$this->recipient = $this->object->get_billing_email();
$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
$this->placeholders['{order_number}'] = $this->object->get_order_number();
$this->placeholders['{full_name}'] = $firstname . ' ' . $lastname;
}
if ( $this->is_enabled() && $this->get_recipient() ) {
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
}
$this->restore_locale();
}
After his I've did this in the content PHP file:
<?php printf( __( get_option( 'wc_custom_email_info' ) ), '{full_name}' ); ?>
In the option I've wrote %s so that I can add the full name into to content. But sadly I'm getting the name of the placeholder and not the content:
Blaaaaaaa {full_name} blaaaa
But I need this here:
Blaaaaaaa Joe Martin blaaaa
Update
The name here is not the customer name from the order. This is a name which I pass through a do_action which I trigger from a button. So when someone on my page clicks this button, I'm fetching his user id and get the name from the user who clicked the button. This is the custom email trigger I use:
$user = get_userdata( get_current_user_id() );
$firstname = $user->user_firstname;
$lastname = $user->last_name;
//Trigger email sending
do_action( 'trigger_email', $order_id, $firstname, $lastname );
Then I do this in the email class:
//Trigger for this email.
add_action( 'trigger_email', array( $this, 'trigger' ), 10, 10 );
After this you can go back to the top trigger function.
Updated
Placeholders in email are only for the email subject in Woocommerce
So what you are trying to do can't work this way.
Instead you will need to make a little change in your trigger() function and adding another method in your class:
/**
* Trigger the sending of this email.
*/
public function trigger( $order_id, $firstname, $lastname, $order = false ) {
$this->setup_locale();
if ( $order_id && ! is_a( $order, 'WC_Order' ) ) {
$order = wc_get_order( $order_id );
}
if ( is_a( $order, 'WC_Order' ) ) {
$this->object = $order;
$this->recipient = $this->object->get_billing_email();
$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
$this->placeholders['{order_number}'] = $this->object->get_order_number();
$this->formatted_name = $firstname . ' ' . $lastname;
}
if ( $this->is_enabled() && $this->get_recipient() ) {
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
}
$this->restore_locale();
}
/**
* Get email content.
*
* #return string
*/
public function get_content() {
$this->sending = true;
if ( 'plain' === $this->get_email_type() ) {
$email_content = preg_replace( $this->plain_search, $this->plain_replace, strip_tags( $this->get_content_plain() ) );
} else {
$email_content = str_replace( '{full_name}', $this->formatted_name, $this->get_content_html() );
}
return wordwrap( $email_content, 70 );
}
This time your placeholder should be replaced with your dynamic $firstname . ' ' . $lastname; when using in your template (content):
<?php printf( get_option( 'wc_custom_email_info' ), '{full_name}' ); ?>
Original answer
Try the following instead that use WC_Order method get_formatted_billing_full_name():
public function trigger( $order_id, $firstname, $lastname, $order = false ) {
$this->setup_locale();
if ( $order_id && ! is_a( $order, 'WC_Order' ) ) {
$order = wc_get_order( $order_id );
}
if ( is_a( $order, 'WC_Order' ) ) {
$this->object = $order;
$this->recipient = $this->object->get_billing_email();
$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
$this->placeholders['{order_number}'] = $this->object->get_order_number();
$this->placeholders['{full_name}'] = $this->object->get_formatted_billing_full_name();
}
if ( $this->is_enabled() && $this->get_recipient() ) {
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
}
$this->restore_locale();
}
It should works. Before on your code $firstname and $lastname were not defined;
I have developed a custom code which sends e-mails to the customers based on the category of the product they buy. The problem with my site is one product is having many categories. The script is working correctly, other than that it sends many emails equal to the number of categories a product have. I want it to send only one e-mail per product.
Here is the code:
add_action("woocommerce_order_status_changed", "wcepp_notification_email");
function wcepp_notification_email( $order_id, $checkout = null ) {
global $woocommerce;
$order = new WC_Order( $order_id );
if($order->status === 'processing' ) {
function wcepp_get_custom_email_html( $order, $heading = false, $mailer, $cat_slug ) {
$template = 'emails/woo-custom-emails-per-product-'.$cat_slug.'.php';
return wc_get_template_html( $template, array(
'order' => $order,
'email_heading' => $heading,
'sent_to_admin' => true,
'plain_text' => false,
'email' => $mailer
)
);
}
$liquify_array = array('cutting-agent', 'dabjuice-liquify');
$terpene_array = array('terpene-blends', 'strain-specific', 'hybrid', 'indica', 'sativa', 'terpene-sample-kits', 'hybrid-sample-kit', 'indica-sample-kit', 'sativa-sample-kit');
$isolates_array = array('raw-terpene-isolates', 'alpha', 'beta');
// getting the order products
$items = $order->get_items();
// let's loop through each of the items
foreach ( $items as $item ) {
$categories = get_the_terms( $item['product_id'] , 'product_cat' );
foreach( $categories as $categorie ) {
$cat_slug = $categorie->slug;
if(in_array($cat_slug, $liquify_array)) {
$new_cat_slug = 'liquify';
} elseif(in_array($cat_slug, $terpene_array)) {
$new_cat_slug = 'terpenes';
} elseif(in_array($cat_slug, $isolates_array)) {
$new_cat_slug = 'isolates';
}
if(isset($new_cat_slug)) {
// Create a mailer
$mailer = $woocommerce->mailer();
//Prepare Email
$recipient = $order->billing_email;
if($new_cat_slug == 'liquify') {
$subject = 'Instructions for usage of your Liquify Product';
} elseif($new_cat_slug == 'terpenes') {
$subject = 'Instructions for usage of your Terpenes Product';
} elseif($new_cat_slug == 'isolates') {
$subject = 'Instructions for usage of your Raw Terpenes Isolates';
}
$content = wcepp_get_custom_email_html( $order, $subject, $mailer, $new_cat_slug );
$headers = "Content-Type: text/html\r\n";
//send the email through wordpress
$mailer->send( $recipient, $subject, $content, $headers );
}
}
}
}
}
Any help in this regard will be appraciated.
Your code is outdated since Woocommerce 3 and have many mistakes. If you enable the debug on Wordpress you will get a lot of error messages. Try the following instead that will send an email for each product when it matches with a defined product category:
// Get email content from the custom template
function get_custom_email_content_html( $order, $heading = false, $mailer, $category_slug ) {
$template = 'emails/woo-custom-emails-per-product-'.$category_slug.'.php';
return wc_get_template_html( $template, array(
'order' => $order,
'email_heading' => $heading,
'sent_to_admin' => true,
'plain_text' => false,
'email' => $mailer
)
);
}
// Send a custom email notification for each order item on order processing status change
add_action( 'woocommerce_order_status_changed', 'processing_custom_email_notification', 10, 4 );
function processing_custom_email_notification( $order_id, $status_from, $status_to, $order ) {
if( $status_to === 'processing' ) {
$liquify_array = array('cutting-agent', 'dabjuice-liquify');
$terpene_array = array('terpene-blends', 'strain-specific', 'hybrid', 'indica', 'sativa', 'terpene-sample-kits', 'hybrid-sample-kit', 'indica-sample-kit', 'sativa-sample-kit');
$isolates_array = array('raw-terpene-isolates', 'alpha', 'beta');
// Loop through order items
foreach ( $order->get_items() as $item ) {
$product_category = '';
$categories_slugs = get_the_terms( $item->get_product_id() , 'product_cat', array( 'fields' => 'slugs' ) );
foreach( $categories_slugs as $term_slug ) {
if( in_array( $term_slug, $liquify_array ) ) {
$product_category = 'liquify';
break;
} elseif(in_array( $term_slug, $terpene_array ) ) {
$product_category = 'terpenes';
break;
} elseif(in_array( $term_slug, $isolates_array ) ) {
$product_category = 'isolates';
break;
}
}
if( isset( $product_category ) ) {
// Email subject
if( $product_category == 'liquify' ) {
$subject = __("Instructions for usage of your Liquify Product");
} elseif($product_category == 'terpenes' ) {
$subject = __("Instructions for usage of your Terpenes Product");
} elseif( $product_category == 'isolates' ) {
$subject = __("Instructions for usage of your Raw Terpenes Isolates");
}
$mailer = WC()->mailer(); // Get an instance of the WC_Emails Object
$recipient = $order->get_billing_email(); // Email recipient
$content = get_custom_email_content_html( $order, $subject, $mailer, $product_category ); // Email content (template)
$headers = "Content-Type: text/html\r\n"; // Email headers
// Send the email
WC()->mailer()->send( $recipient, $subject, $content, $headers );
}
}
}
}
It should better work now (but not tested with your product categories).
3 custom email templates should be located in the woocommerce folder > emails subfolder (of the active theme):
woo-custom-emails-per-product-liquify.php
woo-custom-emails-per-product-terpenes.php
woo-custom-emails-per-product-isolates.php
I have a site that allows users(sellers) to enter products on the frontend. When there product sells I would like to email seller to let them know product has been sold.
Heres what I have so far
add_filter ('woocommerce_payment_complete', 'send_seller_email');
function send_seller_email($order_id) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
//foreach loop to get sellers email from order start
foreach ( $items as $item ) {
$product_name = $item->get_name();
$product_id = $item->get_product_id();
$sent_to = wp_get_object_terms( $product_id, 'soldby', false );
$seller_send_email = $sent_to[0]->name;
$seller_email = get_user_by('ID', $seller_send_email);
$email_to_sent_sold_conformation .= $seller_email->user_email;
}//foreach loop to get sellers email end
//Send seller email start
$to = $email_to_sent_sold_conformation;
$subject = 'Sold! ship now: ' . get_the_title($product_id);
$message = '';
$message .= '<p>' . get_the_excerpt($product_id) . '…</p>';
$message .= '<p></p>';
wp_mail($to, $subject, $message );
//Send seller email end
}
I have completely revisited your code:
The main code is in an utility function called by the 2 hooked functions
function hooked in woocommerce_new_order that will trigger paid orders.
function hooked in woocommerce_order_status_changed that will trigger validated orders on update status change.
The code:
// Utility function sending the email notification
function send_seller_email( $order ) {
$data = array();
// Loop through each order item
foreach ( $order->get_items() as $item_id => $item ) {
if( get_post_meta( $order->get_id(), '_sent_to_seller_'.$item_id, true ) )
continue; // Go to next loop iteration
$product_id = $item->get_product_id();
// Untested part
$soldby = wp_get_object_terms( $product_id, 'soldby', false );
if( empty($soldby) ) continue; // Go to next loop iteration
$seller_id = $soldby[0]->name;
$seller = get_user_by( 'ID', $seller_id );
$seller_email = $seller->user_email;
// Set the data in an array (avoiding seller email repetitions)
$data[$seller_email][] = array(
'excerpt' => get_the_excerpt($product_id),
'title' => get_the_title($product_id),
'link' => get_permalink($product_id),
);
// Update order to avoid notification repetitions
update_post_meta( $order->get_id(), '_sent_to_seller_'.$item_id, true );
}
if( count($data) == 0 ) return;
// Loop through custom data array to send mails to sellers
foreach ( $data as $email_key => $values ) {
$to = $email_key;
$subject_arr = array();
$message = '';
foreach ( $values as $value ) {
$subject_arr[] = $value['title'];
$message .= '<p>'.$value['title'].'</p>';
$message .= '<p>'.$value['excerpt'].'…</p>';
}
$subject = 'Sold! ship now: '.implode( ', ', $subject_arr );
// Send email to seller
wp_mail( $to, $subject, $message );
}
exit();
}
add_action('woocommerce_new_order', 'new_order_seller_notification', 10, 1 );
function new_order_seller_notification( $order_id ) {
$order = wc_get_order( $order_id );
if( ! ( $order->has_status('processing') || $order->has_status('completed') ) )
return; // Exit
send_seller_email( $order );
}
add_action( 'woocommerce_order_status_changed', 'order_status_seller_notification', 20, 4 );
function order_status_seller_notification( $order_id, $status_from, $status_to, $order ) {
if( ! ( $status_to == 'processing' || $status_to == 'completed' ) )
return; // Exit
send_seller_email( $order );
}
Code goes in function.php file of your active child theme (or theme).
The code is not really tested, but doesn't make errors. It should work.
I noticed that the customer on hold order email is not available so i tried to replace the actions with a single action that would send the appropriate email.
This seems to work except for the on-hold status. I dont see what the difference is between the on-hold and processing case other than its not in the $available_emails in class-wc-meta-box-order-actions.php and I have removed all the others and they still work.
What I am doing wrong? Is it a way to make this possible?
My code is:
function ulmh_resend1( $actions ) {
$actions['ulmh_resend'] = __( 'Resend Email', 'text_domain' );
return $actions;
}
function ulmh_resend2( $order ) {
$mailer = WC()->mailer();
$mails = $mailer->get_emails();
if ($order->has_status( 'on-hold' )) {
$eml = 'customer_on_hold_order';
}elseif ($order->has_status( 'processing' )) {
$eml = 'customer_processing_order';
}elseif ($order->has_status( 'completed' )) {
$eml = 'customer_completed_order';
} else {
$eml = "nothing";
}
if ( ! empty( $mails ) ) {
foreach ( $mails as $mail ) {
if ( $mail->id == eml ) {
$mail->trigger( $order->id );
}
}
}
}
function ulmh_resend3( $order_emails ) {
$remove = array( 'new_order', 'cancelled_order', 'customer_processing_order', 'customer_completed_order', 'customer_invoice' );
$order_emails = array_diff( $order_emails, $remove );
return $order_emails;
}
add_action( 'woocommerce_order_actions', 'ulmh_resend1' );
add_action( 'woocommerce_order_action_ulmh_resend', 'ulmh_resend2' );
add_filter( 'woocommerce_resend_order_emails_available', 'ulmh_resend3' );
I have revisited and compacted your code because there where some errors like a typo error in if ( $mail->id == eml ){ for eml as a variable name… Also to get the Order ID from the WC_Order object you should use $order->get_id() method instead of $order->id.
Here is this new functional code:
add_action( 'woocommerce_order_actions', 'ulmh_resend1' );
function ulmh_resend1( $actions ) {
$actions['ulmh_resend'] = __( 'Resend Email', 'text_domain' );
return $actions;
}
add_action( 'woocommerce_order_action_ulmh_resend', 'ulmh_resend2' );
function ulmh_resend2( $order ) {
$wc_emails = WC()->mailer()->get_emails();
if( empty( $wc_emails ) ) return;
if ($order->has_status( 'on-hold' ))
$email_id = 'customer_on_hold_order';
elseif ($order->has_status( 'processing' ))
$email_id = 'customer_processing_order';
elseif ($order->has_status( 'completed' ))
$email_id = 'customer_completed_order';
else
$email_id = "nothing";
foreach ( $wc_emails as $wc_mail )
if ( $wc_mail->id == $email_id )
$wc_mail->trigger( $order->get_id() );
}
add_filter( 'woocommerce_resend_order_emails_available', 'ulmh_resend3' );
function ulmh_resend3( $order_emails ) {
$remove = array( 'new_order', 'cancelled_order', 'customer_processing_order', 'customer_completed_order', 'customer_invoice' );
$order_emails = array_diff( $order_emails, $remove );
return $order_emails;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested in WooCommerce 3+ and works fine now for on-hold order status email notifications, when resending