Last active
July 14, 2021 17:22
-
-
Save justinstern/e707501abe173a57eb4a to your computer and use it in GitHub Desktop.
This snippet will disable WooCommerce core emails based on the product category
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
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 }
@jonlimitless thanks for the suggestions, they've been added
Thanks for this. It works great :-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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