【问题标题】:Disable tax for tax exempt customers in WooCommerce在 WooCommerce 中为免税客户禁用税
【发布时间】:2021-01-01 10:15:25
【问题描述】:

我为我的 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 */
///////////////////////////////////////

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce tax


    【解决方案1】:

    由于 Woocommerce 3 woocommerce_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 文件中。它应该更好地工作。

    相关:Disable tax programmatically for a specific user role

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      • 2018-10-18
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 2018-08-08
      相关资源
      最近更新 更多