检查用户是否在 WooCommerce 电子邮件通知中免征增值税
Check if user is VAT exempt on WooCommerce email notifications
在发送给管理员的 WooCommerce 新订单电子邮件通知中,我试图在用户“免征增值税”时显示“免税订单”附加文本,使用以下内容:
add_action( 'woocommerce_email_before_order_table', 'add_content_specific_email', 20, 4 );
function add_content_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
// Only for admin emails
if ( ! $sent_to_admin ) {
return;
}
if ( $email->id == 'new_order' && !(WC()->customer->get_is_vat_exempt()) && $sent_to_admin ){
return;
}
if ( $email->id == 'new_order' && WC()->customer->get_is_vat_exempt() && $sent_to_admin) {
echo '<h2 class="email-tax-free-title">Tax Free Order</h2></p>';
}
}
在某些情况下,我会收到以下错误:
Uncaught Error: Call to a member function get_is_vat_exempt() on null
#0 /home/site/public_html/wp-includes/class-wp-hook.php(287): add_content_specific_email(Object(Automattic\WooCommerce\Admin\Overrides\Order), true, false, Object(WC_Email_New_Order))
#1 /home/site/public_html/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array)
#2 /home/site/public_html/wp-includes/plugin.php(478): WP_Hook->do_action(Array)
#3 /home/site/public_html/wp-content/plugins/woocommerce/templates/emails/email-order-details.php(22): do_action('woocommerce_ema...', Object(Automattic\WooCommerce\Admin\Overrides\Order), true, false, Object(WC_Email_New_Order))
#4 /home/site/public_html/wp-content/plugins/woocommerce/includes/wc-core-functions.php(344): include('/home/site/...')
#5 /home/site/public_html/wp-content/plugins/woocommerce/includes/class-wc-emails.php(421): wc_get
如何解决这个错误?我的代码有错误吗?
已更新
您的代码中有一些错误。为了避免这个错误,需要检查订单元数据is_vat_exempt
的值是否为'yes',如下:
add_action( 'woocommerce_email_before_order_table', 'new_order_email_is_vat_exempt', 20, 4 );
function new_order_email_is_vat_exempt( $order, $sent_to_admin, $plain_text, $email ) {
// Only for new order admin email notification
if ( $sent_to_admin && 'new_order' === $email->id ) {
// Check if user is VAT exempt for the current order
if( 'yes' === $order->get_meta( 'is_vat_exempt' ) ) {
echo '<h2 class="email-tax-free-title">Tax Free Order</h2></p>';
}
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
在发送给管理员的 WooCommerce 新订单电子邮件通知中,我试图在用户“免征增值税”时显示“免税订单”附加文本,使用以下内容:
add_action( 'woocommerce_email_before_order_table', 'add_content_specific_email', 20, 4 );
function add_content_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
// Only for admin emails
if ( ! $sent_to_admin ) {
return;
}
if ( $email->id == 'new_order' && !(WC()->customer->get_is_vat_exempt()) && $sent_to_admin ){
return;
}
if ( $email->id == 'new_order' && WC()->customer->get_is_vat_exempt() && $sent_to_admin) {
echo '<h2 class="email-tax-free-title">Tax Free Order</h2></p>';
}
}
在某些情况下,我会收到以下错误:
Uncaught Error: Call to a member function get_is_vat_exempt() on null
#0 /home/site/public_html/wp-includes/class-wp-hook.php(287): add_content_specific_email(Object(Automattic\WooCommerce\Admin\Overrides\Order), true, false, Object(WC_Email_New_Order))
#1 /home/site/public_html/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array)
#2 /home/site/public_html/wp-includes/plugin.php(478): WP_Hook->do_action(Array)
#3 /home/site/public_html/wp-content/plugins/woocommerce/templates/emails/email-order-details.php(22): do_action('woocommerce_ema...', Object(Automattic\WooCommerce\Admin\Overrides\Order), true, false, Object(WC_Email_New_Order))
#4 /home/site/public_html/wp-content/plugins/woocommerce/includes/wc-core-functions.php(344): include('/home/site/...')
#5 /home/site/public_html/wp-content/plugins/woocommerce/includes/class-wc-emails.php(421): wc_get
如何解决这个错误?我的代码有错误吗?
已更新
您的代码中有一些错误。为了避免这个错误,需要检查订单元数据is_vat_exempt
的值是否为'yes',如下:
add_action( 'woocommerce_email_before_order_table', 'new_order_email_is_vat_exempt', 20, 4 );
function new_order_email_is_vat_exempt( $order, $sent_to_admin, $plain_text, $email ) {
// Only for new order admin email notification
if ( $sent_to_admin && 'new_order' === $email->id ) {
// Check if user is VAT exempt for the current order
if( 'yes' === $order->get_meta( 'is_vat_exempt' ) ) {
echo '<h2 class="email-tax-free-title">Tax Free Order</h2></p>';
}
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。