#php #wordpress #woocommerce #orders #email-notifications
#php #wordpress #woocommerce #заказы #email-уведомления
Вопрос:
Я хочу остановить уведомление электронной почты 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: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
Любая помощь, пожалуйста?
Спасибо
Ответ №1:
Чтобы избежать этой ошибки, вам нужно добавить эту простую строку внутри вашей функции в начале:
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 файл активной дочерней темы (или активной темы). Теперь это должно сработать.
Ответ №2:
Взгляните на.
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
Комментарии:
1. Спасибо, предоставленный вами код не работает, я все еще получаю электронные письма, предоставленный мной код действительно работает, но вызывает фатальную ошибку (подробности выше).
Ответ №3:
Я удалил (float), чтобы заставить его работать, этот код останавливает уведомление по электронной почте для администратора
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;
}