Saya ingin menghentikan pemberitahuan email woocommerce jika nilai pesanan adalah $0.00

Saya menggunakan kode ini:

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

Kode berfungsi tetapi saya mendapatkan kesalahan fatal ini, semua opsi email menghilang di pengaturan Woocommerce (lihat tangkapan layar terlampir).

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:\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

Mohon bantuannya? Terima kasih

1
Joseph B. 14 Januari 2021, 18:08

3 jawaban

Jawaban Terbaik

Untuk menghindari kesalahan ini, Anda perlu menambahkan baris sederhana ini di dalam fungsi Anda di awal:

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

Saya juga telah meninjau kembali kode Anda sedikit… Coba ini:

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

Kode masuk ke file functions.php dari tema anak aktif (atau tema aktif). Ini harus bekerja sekarang.

0
LoicTheAztec 14 Januari 2021, 17:38

Melihat.

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

Anda dapat menargetkan email yang berbeda woocommerce_email_recipient_customer_processing_order

0
Luke Cavanagh 14 Januari 2021, 15:24

Saya menghapus (mengambang) untuk membuatnya berfungsi, kode ini menghentikan email pemberitahuan untuk 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;
}
0
Joseph B. 14 Januari 2021, 16:50