2016-11-29 12 views
3

注文した内容に関係なく、ユーザーロールに対して税金を適用するこのコードを入手しました。これは問題ありません。特定の製品IDのユーザーロールごとの税率クラス「ゼロレート」

しかし、特定の製品IDに税金無料のを適用する別のユーザーの役割が必要です。これをどのように実現するかわかりません。特定のユーザロールのすべての製品の非課税のために今使用して

コードイムは、次のとおりです。

// Apply a different tax rate based on the user role. 

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

if (in_array('administrator', $current_user_data->roles) || in_array('userrolename', $current_user_data->roles)) 
    $tax_class = 'Zero Rate'; 

return $tax_class; 
} 
add_filter('woocommerce_product_tax_class', 'wc_diff_rate_for_user', 1, 2); 
// Fin Apply a different tax rate based on the user role. 
+0

ロールが必要ですか? –

+0

はい、私は特定の製品IDの –

答えて

3

ここではいくつかの定義された製品と、いくつかの定義されたユーザーの役割のために、この "ゼロ金利" の税クラスを適用するコードです:

add_filter('woocommerce_product_tax_class', 'wc_diff_rate_for_user', 1, 2); 
function wc_diff_rate_for_user($tax_class, $product) { 

    if (is_admin() && ! defined('DOING_AJAX')) 
     return; 

    // Define HERE your targeted products IDs 
    $products_ids_arr = array(12 ,15, 24); 

    // Define HERE your targeted user roles 
    $users_role_arr = array('administrator', 'userrolename'); 

    //Getting the current user data 
    $user_data = get_userdata(get_current_user_id()); 

    foreach ($users_role_arr as $user_role) 
     if (in_array($user_role, $user_data->roles) && in_array($cart_item->id, $products_ids_arr)) { 
      $tax_class = 'Zero Rate'; 
      break; 
     } 

    return $tax_class; 

} 

このコードはテスト済みであり、動作します。

コードは、あなたのアクティブな子テーマ(またはテーマ)の任意のPHPファイル、またはプラグインphpファイルにも含まれます。

+0

あなたは素晴らしい人です、私はまだそれをテストしませんが、 –

+0

はい、私はいくつかの異なるプロジェクトに取り組んでいます。私はたくさんのことに疑念を抱いています。なぜそうですが、すぐに結果を掲載します。 –

0

CASE 1コード

経由あなたは

<?php add_role($role, $display_name, $capabilities); ?> 
よう add role機能を使用することができます

add_role('basic_contributor', 'Basic Contributor', array(
    'read' => true, // True allows that capability 
    'edit_posts' => true, 
    'delete_posts' => false, // Use false to explicitly deny 
)); 

CASE 2:経由プラグイン

https://wordpress.org/plugins/user-role-editor/

+0

に無料の税金を得ることができるように私はちょうど(または2)ユーザーの役割が必要です。こんにちは、情報ありがとう、しかしあなたは私の質問を理解していないようです。私は役割を作成する方法を知っていますが、私が必要とするのは、特定の製品IDに対して税金を免除する特定のユーザー役割です。 –

+0

大丈夫です、あなたは既にそれをコードしています –

+0

https://wordpress.org/support/topic/tax-exempt-checkbox-user-still-charged-tax/ –

関連する問題