簡體   English   中英

根據用戶角色應用不同的稅率

[英]Apply different tax rate based on user role

我使用 WooCommerce 和會員插件。 我為可以免稅或僅繳納特定稅款(在加拿大僅繳納 PST 稅)的客戶創建了特殊稅率類別(CustomerTaxExemptTaxClass 和 CustomerPSTExemptTaxClass。我使用成員插件創建了兩個新角色:客戶免稅和客戶 PST豁免。

我已將以下代碼添加到我的子主題 functions.php 文件中,但是它不起作用。

/* APPLY DIFFERENT TAX RATE BASED ON CUSTOMER USER ROLE */
/* special tax rate: zero if role: Customer Tax Exempt */
function CustomerTaxExemptTaxClass( $tax_class, $product ) {
  global $current_user;

    if (is_user_logged_in() && current_user_can('customer_tax_exempt')) {
        $tax_class = 'CustomerTaxExemptClass';
    }               

  return $tax_class;
}

add_filter( 'woocommerce_product_tax_class', 'CustomerTaxExemptTaxClass', 1, 2 );

/* special tax rate: charge only GST if role: customer_pst_exempt */
function CustomerPSTExemptTaxClass( $tax_class, $product ) {
  global $current_user;

    if (is_user_logged_in() && current_user_can('customer_pst_exempt')) {
        $tax_class = 'CustomerPSTExemptClass';
    }               

  return $tax_class;
}

add_filter( 'woocommerce_product_tax_class', 'CustomerPSTExemptTaxClass', 1, 2 );

我使用了建議的免稅代碼片段,但無論如何我都不是 PHP 專家。

任何幫助,將不勝感激。

謝謝萊瑟

我一直試圖讓這個邏輯工作好幾天。 網上有 3 或 4 個代碼段未正確編碼。 有些甚至會與分析和 Jetpack 產生很大沖突。

本質上,他們中的大多數人對用戶角色進行了錯誤的檢查,或者他們認為用戶將只有一個角色。

這是使用數組對我有用的代碼。 非常基本但有效

/**
 * Tax exempt role logic for "tax_exempt_role"
 */

add_filter( 'init', 'wc_tax_exempt_user_roles' );

function wc_tax_exempt_user_roles() {
    $current_user = wp_get_current_user();
    $current_user_roles = $current_user->roles;
    
    if ( ! is_admin() ) {
        if ( in_array( 'tax_exempt_role', $current_user_roles ) ) {  //<- tax_exempt_rol is my own role name, replace with your
            WC()->customer->set_is_vat_exempt(true);
        } else {
            WC()->customer->set_is_vat_exempt(false);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM