We use this snipped below to unsend the order complete mail for a order IF the order is a Subscription parent. This is working. But I just noticed, that I get a fatal error, when I open the WooCommerce Email settings.
Working solution but with error on WooCommerce eMail setting page:
add_filter( 'woocommerce_email_enabled_customer_completed_order', 'dcwd_conditionally_send_wc_email', 10, 2);
function dcwd_conditionally_send_wc_email( $whether_enabled, $order ) {
//error_log( 'is_enabled: ' . current_filter() . ' - Order: ' . $object->get_order_number() ); // Debugging info.
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->get_type();
//print_r($product_id); die();
// Check for product categories for this item
// two encounter both (variable and normal subscriptions) we need to check both => if ($product_id == 'subscription' || $product_id == 'subscription_variation')
if($product_id == 'subscription_variation') {
return false;
}
}
return $whether_enabled;
}
This is the error, but I do not understand what the problem is…
Fatal error: Uncaught Error: Call to a member function get_items() on null in /home/hosting7878877/www/staging65678.ch/cbcmembership/wp-content/themes/generatepress_child/functions.php:44 Stack trace: #0 /home/hosting7878877/www/staging65678.ch/cbcmembership/wp-includes/class-wp-hook.php(289): dcwd_conditionally_send_wc_email(true, NULL) #1 /home/hosting7878877/www/staging65678.ch/cbcmembership/wp-includes/plugin.php(206): WP_Hook->apply_filters(true, Array) #2 /home/hosting7878877/www/staging65678.ch/cbcmembership/wp-content/plugins/woocommerce/includes/emails/class-wc-email.php(500): apply_filters('woocommerce_ema...', true, NULL, Object(WC_Email_Customer_Completed_Order)) #3 /home/hosting7878877/www/staging65678.ch/cbcmembership/wp-content/plugins/woocommerce/includes/admin/settings/class-wc-settings-emails.php(318): WC_Email->is_enabled() #4 /home/hosting7878877/www/staging65678.ch/cbcmembership/wp-includes/class-wp-hook.php(287): WC_Settings_Emails->email_notification_setting(Array) #5 /home/hosting7878877/www/staging65678.ch/cbcmembership/wp-includes/class-wp-hook.php(311): WP_Hook->apply_fi in /home/hosting7878877/www/staging65678.ch/cbcmembership/wp-content/themes/generatepress_child/functions.php on line 44
2
Answers
I tested your code and it works for me. You can still resolve the error by checking if the variable is an order object:
So:
Apparently, sometimes WooCommerce passes
null
in$order
. Add this condition in order to bypass it:So the final function will be like this: