Остановить уведомление администратора по электронной почте WooCommerce для бесплатных заказов

Я хочу остановить уведомление по электронной почте woocommerce, если стоимость заказа $0.00

Я использовал этот код:

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

Любая помощь, пожалуйста? Спасибо


person Joseph B.    schedule 14.01.2021    source источник


Ответы (3)


Чтобы избежать этой ошибки, вам нужно добавить эту простую строку внутри вашей функции в начале:

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 активной дочерней темы (или активной темы). Теперь он должен работать.

person LoicTheAztec    schedule 14.01.2021
comment
Работал. Большое спасибо. - person Joseph B.; 17.01.2021

Взгляни на.

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

person Luke Cavanagh    schedule 14.01.2021
comment
Спасибо, предоставленный вами код не работает, я все еще получаю электронные письма, предоставленный мной код действительно работает, но вызывает фатальную ошибку (подробности выше). - person Joseph B.; 14.01.2021

Я удалил (плавающий), чтобы он заработал, это письмо с уведомлением об остановке кода для администратора

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;
}
person Joseph B.    schedule 14.01.2021