亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 WooCommerce 中對免稅客戶禁用稅收

在 WooCommerce 中對免稅客戶禁用稅收

PHP
拉丁的傳說 2024-01-19 17:20:55
我為我的 WooCommerce 安裝啟用了稅收。它計算居住在我所在州的所有客戶的稅費(根據他們的送貨地址)。這是 WooCommerce 的默認設置。有些客戶是免稅的,不應繳稅。我在用戶個人資料中創建了一個自定義字段,我可以在其中選中一個框來免除客戶的稅費。這工作正常。我試圖找到一個鉤子,我可以使用該選擇來禁用稅收,但我擁有的代碼導致結帳頁面對所有用戶來說都是空白的。不顯示錯誤消息。我的functions.php代碼如下:////////////////////////////////////////* Tax exempt customers *////////////////////////////////////////// Add tax exempt custom user field in adminadd_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 adminadd_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 checkoutadd_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 *////////////////////////////////////////
查看完整描述

1 回答

?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

由于 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 文件中。它應該更好地工作。


查看完整回答
反對 回復 2024-01-19
  • 1 回答
  • 0 關注
  • 181 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載 公眾號

掃描二維碼
關注慕課網微信公眾號