advanced notifications check for categories including parent

Update the extension to recognize parent categories (currently only recognizes subcategory).

Here’s an example of how this could work:

// Send “New Order” email to different address depending upon product category
// Source: https://stackoverflow.com/questions/54034812/different-recipients-based-on-product-category-in-woocommerce-email-notification
// Custom conditional function that checks for categories (including parent)
// The “successful” email addresses below are added to the addresses in WooCommerce > Settings > Email
/*function has_product_categories( $product_id, $categories ) {
// Initializing
$parent_term_ids = $categories_ids = array();
$taxonomy = ‘product_cat’;
// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
if( is_numeric( $category ) ) {
$categories_ids[] = (int) $category;
} elseif ( term_exists( sanitize_title( $category ), $taxonomy ) ) {
$categories_ids[] = get_term_by( ‘slug’, sanitize_title( $category ), $taxonomy )->term_id;
}
}
// 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(
//’aromagreytown@avalonmarketing.co.nz’ => ‘greytown’,
//’aromasilverstream@avalonmarketing.co.nz’ => ‘silverstream’,
‘aromasilverstream@avalonmarketing.co.nz’ => ’60’,
‘aromagreytown@avalonmarketing.co.nz’ => ’59’,
);
// 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;
}

*/

Author

Current Status

Open

Last updated: May 6, 2020

0 comments

Log in to comment on this feature request.