2016-10-11 91 views
2

在我的woocommerce網站中,我已在一般WooCommerce設置中啓用稅收。以編程方式爲特定用戶角色禁用稅收

我想通過編程方式(使用任何鉤子),從我的商店,結賬頁面和訂單電子郵件禁用特定用戶角色的稅款。

我怎麼能做到這一點?

感謝

回答

4

您不能禁用WooCommerce稅爲特定用戶角色編程,但你可以申請一個特定的用戶角色的零稅率。

首先,您需要在worpress中設置此特定用戶角色。如果是這樣的話,假設這個自定義用戶角色爲'resellers'代表我的代碼示例。

其次,你必須在WooCommerce使設置一個零稅率

enter image description here

然後對每一個國家,你必須設置這個零稅率

enter image description here

第三 - 然後這個函數掛在woocommerce_product_tax_class將這樣的伎倆:

function zero_rate_for_custom_user_role($tax_class, $product) { 
    // Getting the current user 
    $current_user = wp_get_current_user(); 
    $current_user_data = get_userdata($current_user->ID); 

    // <== <== <== <== <== <== <== Here you put your user role slug 
    if (in_array('resellers', $current_user_data->roles)) 
     $tax_class = 'Zero Rate'; 

    return $tax_class; 
} 
add_filter('woocommerce_product_tax_class', 'zero_rate_for_custom_user_role', 1, 2); 

你只需要投入的「經銷商」您想要的用戶角色,而不是塞。

此代碼發送到您活動的子主題(或主題)的function.php文件中,或者也存在於任何插件文件中。

此代碼進行了測試和功能齊全。

參考:WooCommerce - Enabling "Zero rate" tax class to some specific user roles

+0

非常感謝,它的工作。 – Simanto

+0

如果我想禁用非登錄用戶該怎麼辦? –

+0

@LoicTheAztec「零率」對於簡單的產品工作正常,但不適用於Variation產品。請幫忙 – Simanto

相關問題