2017-08-16 1 views
1

Wordpressの特定のユーザーロールのカテゴリのみを表示しようとしています。 このコードは、ログインしていないときや別のユーザーの役割を持っているときにカテゴリの製品を表示していないため、動作します。一部のWoocommerce製品カテゴリとwpmlを非表示にする

しかし、私が抱えている問題は次のとおりです。 ウェブサイトはWPMLを使用しており、私のコードは英語のためだけに働いていました。しかし、他の言語のためではありません。だから同じカテゴリーの別のカテゴリidをテストするために追加しましたが、これはオランダ語のためだけですので、英語とオランダ語で動作することを期待していましたが、英語のものは期待できません。

私が今使っているコードは次のとおりです。

function wholeseller_role_cat($q) { 

// Get the current user 
$current_user = wp_get_current_user(); 

// Displaying only "Wholesale" category products to "whole seller" user role 
if (in_array('wholeseller', $current_user->roles)) { 
    // Set here the ID for Wholesale category 
    $q->set('tax_query', array(
     array(
      'taxonomy' => 'product_cat', 
      'field' => 'id', 
      'terms' => '127,128', // your category ID 
     ) 
    )); 

// Displaying All products (except "Wholesale" category products) 
// to all other users roles (except "wholeseller" user role) 
// and to non logged user. 
} else { 
    // Set here the ID for Wholesale category 
    $q->set('tax_query', array(
     array(
      'taxonomy' => 'product_cat', 
      'field' => 'id', 
      'terms' => '127,128', // your category ID 
      'operator' => 'NOT IN' 
     ) 
    )); 
} 
} 
add_action('woocommerce_product_query', 'wholeseller_role_cat'); 

だから、英語のカテゴリIDは127で、オランダ語のためには、128 だ誰かが、これが再び作業を取得するために私を助けることができますか?

誰もが私にこれを手伝うことができますか?


更新

ユーザの役割がWholesellerとき、英語とオランダ語の言語は今だけのカテゴリを示しています。しかし私は私のウェブサイトにいくつかの言語を追加しています。

ここでは、対応するカテゴリIDとの完全なリストは、次のとおりです。

English (en) => 117 
Dutch (nl) ===> 118 
French (fr) ==> 131 
Italian (it) => 134 
Spanish (es) => 137 
German (de) ==> 442 

どのように私はそれが2つの以上の言語のために働くことができますか?

答えて

1

アップデート2(完全WPML検出言語コード/製品カテゴリID)

  1. 私は(関連文書linkedを参照してください)'field' => 'term_id''field' => 'id'を交換した:
  2. 私はWPML定数ICL_LANGUAGE_CODEを使用言語を検出し、適切なカテゴリを設定します。たとえば、あなたがサイトの "英語"バージョンを使用している場合、商品カテゴリIDは127となり、オランダ語の場合は128となります。言語コードとカテゴリIDは、コードの先頭)。
  3. 私はコードを圧縮しました。ここで

更新され、圧縮されたコードです:

add_action('woocommerce_product_query', 'wpml_product_category_and_user_role_condionnal_filter', 10, 1); 
function wpml_product_category_and_user_role_condionnal_filter($q) { 

    // Set HERE this indexed array for each key (as language code in 2 letters)… 
    // …and the corresponding category ID value (numerical) 
    $lang_cat_id = array('en' => 127, 'nl' => 128, 'fr' => 131, 'it' => 134, 'es' => 137, 'de' => 442,); 

    // With WPML constant "ICL_LANGUAGE_CODE" to detect the language and set the right category 
    foreach($lang_cat_id as $lang => $cat_id) 
     if(ICL_LANGUAGE_CODE == $lang) $category_id = $cat_id; 

    // Get the current user (WP_User object) 
    $current_user = wp_get_current_user(); 

    // Displaying only "Wholesale" product category to "whole seller" user role (IN) 
    // or displaying other product categories to all other user roles (NOT IN) 
    $operator = in_array('wholeseller', $current_user->roles) ? 'IN' : 'NOT IN' ; 

    // The conditional query 
    $q->set('tax_query', array(array(
     'taxonomy' => 'product_cat', 
     'field' => 'item_id', // Replaced 'id' by 'term_id' (see documentation) 
     'terms' => $category_id, // Id depending on language 
     'operator' => $operator // Depending on user roles 
    ))); 
} 

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

これは多言語WPML製品カテゴリIDでテストされ、動作します。


関連資料:これで私を助けるためWP_Query Taxonomy Parameters

+0

ありがとう! functions.phpにコードを追加しましたが、機能しません。私は、ショップページに隠れたカテゴリの製品をオランダ語で見続けています。他のカテゴリのIDを追加しようとしましたが、まだ動作していません。何か案は? @LoicTheAztec – 123MijnWebsite

+0

ありがとうございました! – 123MijnWebsite

+0

こんにちは@LoiTheAztecありがとうございました!唯一のことは、ユーザーの役割がwholesellerでない場合、ID 127のカテゴリのみを表示するということです。しかし、逆にする必要がありますか?だから、ユーザーロールがwholesellerの場合は、カテゴリ127,128のみを表示する必要があります。 – 123MijnWebsite

関連する問題