skip to Main Content

I want to stop woocommerce email notification if order value is $0.00

I used this code :

function restrict_admin_new_order_mail( $recipient, $order ) {
    if( $order->get_total() === '0.00' ) {
        return;
    } else {
        return $recipient;
    }
}
add_filter('woocommerce_email_recipient_new_order', 'restrict_admin_new_order_mail', 1, 2);

The code working but I got this fatal error, all email options disappeared at Woocommerce settings (see attached screenshot).

screenshot

Error Details
=============

[13-Jan-2021 17:34:15 UTC] PHP Fatal error:  Uncaught Error: Call to a member function get_total() on null in C:UsersjoeLocal Sitesstagingapppublicwp-contentthemesflatsome-childfunctions.php:8
Stack trace:
#0 C:UsersjoeLocal Sitesstagingapppublicwp-includesclass-wp-hook.php(289): restrict_admin_new_order_mail('email@gmail....', NULL)
#1 C:UsersjoeLocal Sitesstagingapppublicwp-includesplugin.php(206): WP_Hook->apply_filters('email@gmail....', Array)
#2 C:UsersjoeLocal Sitesstagingapppublicwp-contentpluginswoocommerceincludesemailsclass-wc-email.php(399): apply_filters('woocommerce_ema...', 'email@gmail....', NULL, Object(WC_Email_New_Order))
#3 C:UsersjoeLocal Sitesstagingapppublicwp-contentpluginswoocommerceincludesadminsettingsclass-wc-settings-emails.php(294): WC_Email->get_recipient()
#4 C:UsersjoeLocal Sitesstagingapppublicwp-includesclass-wp-hook.php(287): WC_Settings_Emails->email_notification_setting(Array)
#5 C:UsersjoeLocal Sitesstagingapppublicwp-includes in C:UsersjoeLocal Sitesstagingapppublicwp-contentthemesflatsome-childfunctions.php on line 8

Any help please?
Thanks

3

Answers


  1. Chosen as BEST ANSWER

    I removed (float) to make it work, this code stop notification email for admin

    add_filter( 'woocommerce_email_recipient_new_order', 'wc_disable_customer_order_email_if_free', 10, 2 );
    
    function wc_disable_customer_order_email_if_free( $recipient, $order ) {
        $page = $_GET['page'] = isset( $_GET['page'] ) ? $_GET['page'] : '';
        if ( 'wc-settings' === $page ) {
            return $recipient; 
        }
        if ( $order->get_total() === '0.00' ) $recipient = '';
        return $recipient;
    }
    

  2. Take a look at.

    add_filter( 'woocommerce_email_recipient_customer_completed_order', 'wc_disable_customer_order_email_if_free', 10, 2 );
      
    function wc_disable_customer_order_email_if_free( $recipient, $order ) {
        $page = $_GET['page'] = isset( $_GET['page'] ) ? $_GET['page'] : '';
        if ( 'wc-settings' === $page ) {
            return $recipient; 
        }
        if ( (float) $order->get_total() === '0.00' ) $recipient = '';
        return $recipient;
    }
    

    You can target different emails woocommerce_email_recipient_customer_processing_order

    Login or Signup to reply.
  3. To avoid this error, you need to add this simple line inside your function at the beginning:

    if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
    

    I have also revisited your code a bit… Try this:

    add_filter('woocommerce_email_recipient_new_order', 'restrict_admin_new_order_mail', 1, 2);
    function restrict_admin_new_order_mail( $recipient, $order ) {
        if ( ! is_a( $order, 'WC_Order' ) ) 
            return $recipient;
    
        if( $order->get_total() > 0 ) {
            return $recipient;
        } else {
            return '';
        }
    }
    

    Code goes in functions.php file of the active child theme (or active theme). It should works now.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search