Different Recipients Based on Product Category in Woocommerce Email Notification

Different recipients based on product category in WooCommerce email notification

The following will add additional recipients to "New Order" email notification based on your product categories, that you will define in an indexed array with an email recipient / product category pairs:

add_filter( 'woocommerce_email_recipient_new_order', 'custom_email_recipient_new_order', 10, 2 );
function custom_email_recipient_new_order( $recipient, $order ) {
// Not in backend when using $order (avoiding an error)
if( ! is_a($order, 'WC_Order') ) return $recipient;

// Define the email recipients / categories pairs in the array
$recipients_categories = array(
'email.one@email.com' => 'category-one',
'email.two@email.com' => 'category-two',
'email.three@email.com' => 'category-three',
);

// Loop through order items
foreach ( $order->get_items() as $item ) {
// Loop through defined product categories
foreach ( $recipients_categories as $email => $category ) {
if( has_term( $category, 'product_cat', $item->get_product_id() ) && strpos($recipient, $email) === false ) {
$recipient .= ',' . $email;
}
}
}
return $recipient;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Notes:

  • The defined Product categories can be term Ids, term slugs or term names.
  • Each product category need to be defined in the related products as has_term() WordPress conditional function doesn't handle parent terms.

###Addition to handle parent product categories:

// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
$parent_term_ids = $categories_ids = array(); // Initializing
$taxonomy = 'product_cat';
$product_id = $product_id == 0 ? get_the_id() : $product_id;

if( is_string( $categories ) ) {
$categories = (array) $categories; // Convert string to array
}

// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
$result = (array) term_exists( $category, $taxonomy );
if ( ! empty( $result ) ) {
$categories_ids[] = reset($result);
}
}

// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}

// Adding custom recipients based on product categories
add_filter( 'woocommerce_email_recipient_new_order', 'custom_email_recipient_new_order', 10, 2 );
function custom_email_recipient_new_order( $recipient, $order ) {
// Not in backend when using $order (avoiding an error)
if( ! is_a($order, 'WC_Order') ) return $recipient;

// Define the email recipients / categories pairs in the array
$recipients_categories = array(
'email.one@email.com' => 'category-one',
'email.two@email.com' => 'category-two',
'email.three@email.com' => 'category-three',
);

// Loop through order items
foreach ( $order->get_items() as $item ) {
// Loop through defined product categories
foreach ( $recipients_categories as $email => $category ) {
if( has_product_categories( $item->get_product_id(), array( $category ) ) && strpos($recipient, $email) === false ) {
$recipient .= ',' . $email;
}
}
}
return $recipient;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Note: The Product categories can be term Ids, term slugs or term names.


Similar: Different recipients based on products sold in WooCommerce email notification

Different recipients based on country for WooCommerce new order email notification

Please never edit core files!

When you modify core files you run the risk of breaking the plugin and possibly your WordPress installation. In addition, it makes it impossible for the plugin developer to provide support for you since they have no knowledge of what you’ve changed.

Use instead the woocommerce_email_recipient_{$email_id} filter composite hook, where {$email_id} = new_order

So you get:

function filter_woocommerce_email_recipient_new_order( $recipient, $order, $email ) {
// Avoiding backend displayed error in WooCommerce email settings
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;

// Get shipping country
$country = $order->get_shipping_country();

// When empty
if ( empty( $country ) ) {
// Get billing country
$country = $order->get_billing_country();
}

// Checks if a value exists in an array
if ( in_array( $country, array( 'IL' ) ) ) {
$recipient = 'abc@example.com';
} elseif ( in_array( $country, array( 'FR', 'BE', 'LU', 'NL', 'IT', 'PT', 'ESP' ) ) ) {
$recipient = 'def@example.com, ghi@example.com';
} else {
$recipient = 'jkl@example.com';
}

return $recipient;
}
add_filter( 'woocommerce_email_recipient_new_order', 'filter_woocommerce_email_recipient_new_order', 10, 3 );

Conditional text based on product category in WooCommerce customer completed order email notification

Your code has some minor errors, following code/answer contains:

  1. Only for customer order completed email
  2. IF the order has only products in cat-one THEN use "SOME TEXT"
  3. IF the order has products in cat-one AND products from any other categories THEN use "SOME OTHER TEXT"
  4. IF the order has products from ALL categories except cat-one THEN use "ANOTHER TEXT"

So you get:

function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {   
// Only for order completed email
if ( $email->id == 'customer_completed_order' ) {
// Booleans
$cat_one = false;
$cat_two = false;
$cat_three = false;
$cat_four = false;

// Output
$output = '';

// Loop through order items
foreach ( $order->get_items() as $item ) {
// Product ID
$product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();

// Has term (product category)
if ( has_term( 'cat-one', 'product_cat', $product_id ) ) {
$cat_one = true;
}

if ( has_term( 'cat-two', 'product_cat', $product_id ) ) {
$cat_two = true;
}

if ( has_term( 'cat-three', 'product_cat', $product_id ) ) {
$cat_three = true;
}

if ( has_term( 'cat-four', 'product_cat', $product_id ) ) {
$cat_four = true;
}
}

// 1. ONLY CAT-ONE
if ( $cat_one ) {
$output = "SOME TEXT";

// 2. CAT-ONE and any other Category
if ( $cat_two || $cat_three || $cat_four ) {
$output = "SOME OTHER TEXT";
}
} else {
// 3. ALL CATEGORIES EXCEPT CAT-ONE
if ( $cat_two && $cat_three && $cat_four ) {
$output = "ANOTHER TEXT";
}
}

// Output
echo $output;
}
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 10, 4 );

Display a custom text based on product category in WooCommerce Email

Try the following:

add_action( 'woocommerce_email_before_order_table', 'email_before_order_table_conditional_display', 20, 4 );
function email_before_order_table_conditional_display( $order, $sent_to_admin, $plain_text, $email ) {
// Only for "commpeted" order status notification
if ( 'customer_completed_order' !== $email->id ) return;

foreach( $order->get_items() as $item ){
if( has_term( "Gifts", "product_cat", $item->get_product_id() ) ){
echo '<p class="email-text-conditional">Thank you for your order with Adventure Clues, we have sent your recipient the gift card.</p>';
break;
}
if( has_term( "Clothes", "product_cat", $item->get_product_id() ) ){
echo '<p class="email-text-conditional">Thank you for your order with Adventure Clues, we are managing your Clothes.</p>';
break;
}
}
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Add product category to WooCommerce Email Notifications

You can use implode() function that join array values in a string.

I also added an extra check for empty values

function render_product_description( $item_id, $item, $order, $plain_text ) {
// Get product id
$product_id = $item->get_product_id();

// Get product
$product = $item->get_product();

// Product content
$product_content = $product->post->post_content;

// NOT empty
if ( ! empty ( $product_content ) ) {
echo '<p>' . $product_content . '</p>';
}

// Get post meta
$time = get_post_meta( $product_id, 'time', true );

// NOT empty
if ( ! empty ( $time ) ) {
echo '<p><strong>Date:</strong><br />' . $time . '</p>';
}

// Get terms
$term_names = wp_get_post_terms( $product_id, 'product_cat', ['fields' => 'names'] );

// NOT empty
if ( ! empty ( $term_names ) ) {
echo '<p><strong>Categories:</strong><br />' . implode( ", ", $term_names ) . '</p>';
}

// Get post meta
$date = get_post_meta( $product_id, 'date', true );

// NOT empty
if ( ! empty ( $date ) ) {
echo '<p><strong>Date:</strong><br />' . $date . '</p>';
}
}

add_action( 'woocommerce_order_item_meta_end', 'render_product_description', 10, 4 );

Send email if woocommerce order has items from a specific product category

The code below will send a custom email to customer for a specific product category when order status is changed to "completed":

// On order completed status
add_action('woocommerce_order_status_completed', 'send_a_custom_email', 20, 1 );
function send_a_custom_email( $order_id ) {
$order = wc_get_order( $order_id ); // The WC_Order object

foreach ( $order->get_items() as $item_id => $item ) {
$product = $item->get_product(); // The WC_Product object

// Get the parent product ID for product variations for product categories
$the_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

if ( has_term( array( 630 ), 'product_cat', $the_id ) ) {
$to_email = $order->get_billing_email(); // To customer
$subject = "Your subject goes here";
$message = "Your message goes here";
$headers = 'From: Shop Name <shop@email.com>' . "\r\n"; // From admin email

wp_mail( $to_email, $subject, $message, $headers ); // Send email
break; // Stop the loop
}
}
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works



Related Topics



Leave a reply



Submit