I am hosting a webshop selling both merchandise and event tickets, using WooCommerce in combination with FooEvents.
I am having issues with the 'customer order completed' emails:
- I want to send them for Merchandise items (order ready to pick up)
- but do not want to send them for tickets (since these are issued automatically by email)
I tried to conditionally send the email notification based on the product categories, based on the following code:
(taken from this post: Avoid customer email notification for a specific product category in Woocommerce)
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'product_cat_avoid_processing_email_notification', 10, 2 );
function product_cat_avoid_processing_email_notification( $recipient, $order ) {
if( is_admin() ) return $recipient;
// HERE set your product categories (coma separated term Ids, slugs or names)
$cat_to_filter = 'tickets';
// 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 categories for this item
if( has_term( $cat_to_filter, 'product_cat', $product_id ) )
return ''; // If it's found, we return an empty recipient
}
return $recipient;
}
This code works fine for excluding orders with regular products, e.g. I can exclude order completed notifications for the product category 'merchandise'. However, for tickets it does not work and results in an error: a generic error message on checkout page, the order is created but tickets are not generated nor sent.
My hunch is, that the code does not work with FooEvents, either because:
- the code deletes the customer email address FooEvents needs to send the tickets, or
- the code interferes with FooEvents in the area of changing order statusses to 'completed'
Can you help me out and provide me a fix for this issue?
Thanks,
Jeroen