php – 如何根据用户角色隐藏具有给定类别的WooCommerce产品
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 如何根据用户角色隐藏具有给定类别的WooCommerce产品,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3067字,纯文字阅读大概需要5分钟。
内容图文
![php – 如何根据用户角色隐藏具有给定类别的WooCommerce产品](/upload/InfoBanner/zyjiaocheng/819/8326e1965e7f45f9888e0c5b3935978e.jpg)
我有一个客户端有以下问题:
我需要能够将他们的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.
}
}
我浏览过StackOverflow,但是我还没找到我正在寻找的内容,或者非常清楚我应该使用哪些关键字进行搜索.
你能为我指出正确的方向吗?
解决方法:
对的,这是可能的.有两种方式:
1)使用在创建查询变量对象之后但在运行实际查询之前调用的pre_get_posts wordpress钩子.所以它非常适合这种情况.我们在这里想象’批发’类别的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钩子. (我们仍然在这里想象’批发’类别的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文件中,或者更好地在自定义插件中.
如果要使用类别slug而不是类别ID,则必须部分替换(两个数组):
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'wholesale', // your category slug (to use the slug see below)
您可以根据需要添加,在if语句中使用some woocommerce conditionals tags来限制更多.
参考文献:
> Modifying the WooCommerce Product Query
> I can’t fetch WooCommerce products by category id
> WordPress Class Reference – WP Query
内容总结
以上是互联网集市为您收集整理的php – 如何根据用户角色隐藏具有给定类别的WooCommerce产品全部内容,希望文章能够帮你解决php – 如何根据用户角色隐藏具有给定类别的WooCommerce产品所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。