Skip to content

Instantly share code, notes, and snippets.

@justinstern
Last active July 14, 2021 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinstern/e707501abe173a57eb4a to your computer and use it in GitHub Desktop.
Save justinstern/e707501abe173a57eb4a to your computer and use it in GitHub Desktop.
This snippet will disable WooCommerce core emails based on the product category
<?php
// Add the below to the bottom of your theme's functions.php:
add_filter( 'woocommerce_email_enabled_customer_processing_order', 'disable_emails_for_fundraising', 10, 2 );
add_filter( 'woocommerce_email_enabled_customer_completed_order', 'disable_emails_for_fundraising', 10, 2 );
function disable_emails_for_fundraising( $enabled, $order ) {
if ( isset( $order ) && count( $order->get_items() ) > 0 ) {
foreach( $order->get_items() as $item ) {
if ( 'line_item' == $item['type'] ) {
$product = $order->get_product_from_item( $item );
foreach ( array( 'fundraising', 'cat2' ) as $category ) {
if ( has_term( $category, 'product_cat', $product->get_post_data() ) ) {
return false;
}
}
}
}
}
return $enabled;
}
@ToggoStar
Copy link

Hi there,

thank you for the snippet! However, I think there is a small mistake - you have one too many closing brackets, don't you? The one in line 24 should be removed, right?

Cheers

@jonlimitless
Copy link

Hi there,

thank you for the snippet! However, I think there is a small mistake - you have one too many closing brackets, don't you? The one in line 24 should be removed, right?

Cheers

Actually need to be an open bracket on line 17 at the end of foreach()

Note: This filter is now used for other areas of Woocommerce. It is suggest to add the if check listed below on line 9 to prevent issues.
if( isset($order) && $order !== null ){

Of course add an ending bracket on line 25 }

@justinstern
Copy link
Author

@jonlimitless thanks for the suggestions, they've been added

@sdesign75
Copy link

Thanks for this. It works great :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment