I'm using the following code, but it's not working, WooCommerce is still sending emails when the order changes from processing to completed for those two product tags.
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'product_tag_avoid_completed_email_notification', 10, 2 );
function product_tag_avoid_completed_email_notification( $recipient, $order ) {
if ( is_admin() ) {
return $recipient;
}
// HERE set your product tags (comma-separated slugs)
$disabled_tags = array( 'special-promo', 'regular' );
// Loop through order items
foreach ( $order->get_items() as $item ) {
// Get an instance of the WC_Product object
$product = $item->get_product();
// Get the correct product ID (for variations we take the parent product ID)
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
// Check for product tags for this item
$tags = wc_get_product_term_ids( $product_id, 'product_tag' );
// Check if any of the disabled tags are present in the product tags
if ( array_intersect( $disabled_tags, $tags ) ) {
return ''; // If any of the specified tags are found, we return an empty recipient
}
}
return $recipient;
}
I want to disable the customer email sending when an order change from processing to completed with products tagged with " special promo" or "Regular"
There are some mistakes and complications in your code… Try the following simplified revised code:
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'product_tag_avoid_completed_email_notification', 10, 2 );
function product_tag_avoid_completed_email_notification( $recipient, $order ) {
if ( is_a( $order, 'WC_Order' ) ) {
// HERE set your product tags (comma-separated term names, slugs or Ids)
$targeted_terms = array('special-promo', 'regular');
$targeted_taxonomy = 'product_tag';
// Loop through order items
foreach ( $order->get_items() as $item ) {
// Check for matching product tags
if ( has_term( $targeted_terms, $targeted_taxonomy, $item->get_product_id() ) ) {
return ''; // return empty recipient avoiding this email notification
}
}
}
return $recipient;
}
Code goes in functions.php file of your child theme (or in a plugin). It should work.