停止免费订单的 WooCommerce 管理员电子邮件通知

Stop WooCommerce admin email notification for free orders

如果订单值为 [=12=].00

,我想停止 woocommerce 电子邮件通知

我使用了这段代码:

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);

代码有效,但我遇到了这个致命错误,所有电子邮件选项在 Woocommerce 设置中都消失了(参见随附的屏幕截图)。

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:\Users\joe\Local Sites\staging\app\public\wp-content\themes\flatsome-child\functions.php:8
Stack trace:
#0 C:\Users\joe\Local Sites\staging\app\public\wp-includes\class-wp-hook.php(289): restrict_admin_new_order_mail('email@gmail....', NULL)
#1 C:\Users\joe\Local Sites\staging\app\public\wp-includes\plugin.php(206): WP_Hook->apply_filters('email@gmail....', Array)
#2 C:\Users\joe\Local Sites\staging\app\public\wp-content\plugins\woocommerce\includes\emails\class-wc-email.php(399): apply_filters('woocommerce_ema...', 'email@gmail....', NULL, Object(WC_Email_New_Order))
#3 C:\Users\joe\Local Sites\staging\app\public\wp-content\plugins\woocommerce\includes\admin\settings\class-wc-settings-emails.php(294): WC_Email->get_recipient()
#4 C:\Users\joe\Local Sites\staging\app\public\wp-includes\class-wp-hook.php(287): WC_Settings_Emails->email_notification_setting(Array)
#5 C:\Users\joe\Local Sites\staging\app\public\wp-includes\ in C:\Users\joe\Local Sites\staging\app\public\wp-content\themes\flatsome-child\functions.php on line 8

有什么帮助吗? 谢谢

看看。

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;
}

您可以定位不同的电子邮件 woocommerce_email_recipient_customer_processing_order

我删除了(浮动)以使其工作,此代码停止了管理员的通知电子邮件

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;
}

为避免此错误,您需要在函数开头添加以下简单行:

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

我也重新审视了你的代码......试试这个:

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 '';
    }
}

代码进入活动子主题(或活动主题)的 functions.php 文件。现在应该可以使用了。