如何隐藏WooCommerce产品具有基于用户角色

问题描述:

我有以下问题客户端的特定类别:如何隐藏WooCommerce产品具有基于用户角色

我需要能够把自己的WooCommerce WordPress站点分成两个基本类别。如果用户以"Wholeseller"登录,则仅从"Wholesale"类别中的产品被从数据库中拉出。

但是,如果用户没有登录或已经登录,但不是"Wholeseller",则只有产品无的"Wholesale"类别获得从数据库中抽取。

我想我会喜欢这个添加了一些主题的functions.php文件:

add_filter("some_woocommerce_hook", "wholeseller_filter"); 

function wholeseller_filter() { 
    if (current_user->role == "Wholeseller"){ 
     //omit products without "wholesale" category while browsing whole site 
    } else { 
     //omit products with "wholesale" in the category while browsing whole site. 
    } 
} 

我周围*上浏览,但我还没有找到我要找的还是很清楚什么是关键词我应该使用我的搜索。

您能否指点我正确的方向?

是的,这是可能的。有两种方法:

1)与pre_get_posts在创建查询变量对象之后但在运行实际查询之前调用的wordpress挂钩。所以对于这种情况非常适合。我们在这里想象一下,'Wholesale'类别的ID是'123'

下面是自定义代码:

function wholeseller_role_cat($query) { 

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

    if ($query->is_main_query()) { 
     // Displaying only "Wholesale" category products to "whole seller" user role 
     if (in_array('wholeseller', $current_user->roles)) { 
      // Set here the ID for Wholesale category 
      $query->set('cat', '123'); 

     // 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 (with minus sign before) 
      $query->set('cat', '-123'); // negative number 
     } 
    } 
} 
add_action('pre_get_posts', 'wholeseller_role_cat'); 

这一代码进入你的活动的子主题或主题,或自定义插件更好的function.php文件。


2)同woocommerce_product_query WooCommerce钩。 (我们在这里还是想象一下,'Wholesale'类别的ID是'123'

下面是自定义代码:

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' => 'term_id', 
       'terms' => '123', // 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' => 'term_id', 
       'terms' => '123', // your category ID 
       'operator' => 'NOT IN' 
      ) 
     )); 
    } 
} 
add_action('woocommerce_product_query', 'wholeseller_role_cat'); 

这一代码进入你的活动的子主题或主题,或自定义插件更好的function.php文件。

如果你要使用的类别塞代替类别ID,你将不得不与部分替代(都是数组):

  array(
       'taxonomy' => 'product_cat', 
       'field' => 'slug', 
       'terms' => 'wholesale', // your category slug (to use the slug see below) 

,如果你愿意,你需要你可以添加,some woocommerce conditionals tagsif statements甚至更​​多地限制。

参考文献:

+0

'pre_get_posts'所有的东西!虽然我会建议不要将它保留在主题的'functions.php'中,而应将其移入插件中。 – helgatheviking

+1

'functions.php'是最简单的放置快速测试片段的地方,但我总是建议使用您的主题来显示相关的功能,并在插件中保留任何功能特定的代码。 – helgatheviking

+0

您不应该在pre_get_posts上运行此操作,除非您希望在每个查询中运行此操作。 Woocommerce钩子“woocommerce_product_query”将允许您仅在woocommerce查询中运行它。 –

为了使这个更好的性能和对woocommerce查询使用下面的函数和挂钩才有效。

function exclude_product_from_wholesale($q){ 

$current_user = wp_get_current_user(); 
$ARRAY_OF_PRODUCT_IDS_YOU_WANT_HIDDEN = array(); 

if (in_array('wholeseller', $current_user->roles)) { 
    $q->set('post__not_in', $ARRAY_OF_PRODUCT_IDS_YOU_WANT_HIDDEN); 
} 

} 

add_action('woocommerce_product_query', 'exclude_product_from_wholesale'); 

你可以放弃这个简单的函数到你的functions.php。