首页 > 解决方案 > 在 WooCommerce 中为免税客户禁用税

问题描述

我为我的 WooCommerce 安装启用了税收。它计算居住在我所在州的所有客户的税款(根据他们的送货地址)。这是 WooCommerce 的默认设置。

有些客户是免税的,不应该被征税。我在用户个人资料中创建了一个自定义字段,我可以在其中选中一个框来免除客户的税款。这可以正常工作。

我试图找到一个挂钩,我可以在其中使用该选择来禁用税收,但我拥有的代码导致结帐页面对于所有用户都是空白的。不显示错误消息。

我的functions.php代码如下:

///////////////////////////////////////
/* Tax exempt customers */
///////////////////////////////////////

// Add tax exempt custom user field in admin
add_action( 'show_user_profile', 'add_customer_tax_exempt_checkbox', 10 );
add_action( 'edit_user_profile', 'add_customer_tax_exempt_checkbox', 10 );
function add_customer_tax_exempt_checkbox( $user )
{
    ?>
    <h3><?php _e("Tax status"); ?></h3>
    <table class="form-table">
        <tr>
            <th><?php _e("Tax exempt"); ?></th>
            <td>
    <?php

    woocommerce_form_field( 'tax_exempt', array(
        'type'      => 'checkbox',
        'class'     => array('input-checkbox'),
        'label'     => __('Allowed'),
    ), get_user_meta( $user->id, 'tax_exempt', true ) );

    ?>
            </td>
        </tr>
    </table>
    <?php
}

// Save allowed custom user field in admin
add_action( 'personal_options_update', 'save_customer_tax_exempt_checkbox' );
add_action( 'edit_user_profile_update', 'save_customer_tax_exempt_checkbox' );
function save_customer_tax_exempt_checkbox( $user_id )
{
    if ( current_user_can( 'edit_user', $user_id ) ) {
        update_user_meta( $user_id, 'tax_exempt', isset($_POST['tax_exempt']) ? '1' : '0' );
    }
}

// Enabling or disabling tax calculation at checkout
add_filter( 'woocommerce_product_tax_class', 'disable_tax_calculation' );
function disable_tax_calculation( $tax_class, $product ) {
   if ( get_user_meta( get_current_user_id(), 'tax_exempt', true ) ) {
        $tax_class = 'Zero Rate';
   }
   return $tax_class;
}

///////////////////////////////////////
/* END: Tax exempt customers */
///////////////////////////////////////

标签: phpwordpresswoocommercehook-woocommercetax

解决方案


由于 Woocommerce 3woocommerce_product_tax_class钩子已被弃用并已被替换。我在下面更新了您的第三个功能:

// Add tax exempt custom user field in admin
add_action( 'show_user_profile', 'add_customer_tax_exempt_checkbox', 10 );
add_action( 'edit_user_profile', 'add_customer_tax_exempt_checkbox', 10 );
function add_customer_tax_exempt_checkbox( $user )
{
    ?>
    <h3><?php _e("Tax status"); ?></h3>
    <table class="form-table">
        <tr>
            <th><?php _e("Tax exempt"); ?></th>
            <td>
    <?php

    woocommerce_form_field( 'tax_exempt', array(
        'type'      => 'checkbox',
        'class'     => array('input-checkbox'),
        'label'     => __('Allowed'),
    ), get_user_meta( $user->id, 'tax_exempt', true ) );

    ?>
            </td>
        </tr>
    </table>
    <?php
}

// Save allowed custom user field in admin
add_action( 'personal_options_update', 'save_customer_tax_exempt_checkbox' );
add_action( 'edit_user_profile_update', 'save_customer_tax_exempt_checkbox' );
function save_customer_tax_exempt_checkbox( $user_id )
{
    if ( current_user_can( 'edit_user', $user_id ) ) {
        update_user_meta( $user_id, 'tax_exempt', isset($_POST['tax_exempt']) ? '1' : '0' );
    }
}

// Enabling or disabling tax calculation at checkout
add_filter( 'woocommerce_product_get_tax_class', 'disable_tax_calculation', 10, 2 );
add_filter( 'woocommerce_product_variation_get_tax_class', 'disable_tax_calculation', 10, 2 );
function disable_tax_calculation( $tax_class, $product ) {
   if ( get_user_meta( get_current_user_id(), 'tax_exempt', true ) ) {
        $tax_class = 'Zero Rate';
   }
   return $tax_class;
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。它应该更好地工作。

相关:以编程方式为特定用户角色禁用税收


推荐阅读