简体   繁体   English

如何从特定用户角色重定向 WooCommerce 产品的结帐页面

[英]How to redirect WooCommerce product's checkout page from a specific user role

I have three user roles General users, pro users and Ultra pro users我有三个用户角色普通用户、专业用户和超级专业用户

i have categorized the product in general, pro and ultra product我将产品分为一般产品、专业产品和超产品

now want to redirect on specific page accordingly现在想相应地重定向到特定页面

general user cannot see checkout page for pro and ultra products一般用户看不到 pro 和 ultra 产品的结帐页面
pro wont see checkout page for ultra pro pro不会看到 ultra pro 的结帐页面
ultra pro can buy anything ultra pro可以买任何东西

add_action( 'wp', 'hide_product_from_specific_role' );
function hide_product_from_specific_role() {
  if ( is_product() ) {
    $product_id = get_the_ID();
    $user = wp_get_current_user();

    if ( in_array( 'specific_role', (array) $user->roles ) ) {
      wp_redirect( home_url() );
      exit;
    }
  }
}

how can i use this code if this is incorrect, what should be the code?如果这不正确,我该如何使用此代码,代码应该是什么?

I see what you are looking for, I had a similar project and I think that your code is missing an if statment to check if the user logged in and I think you beeter leave a msg to explain why the user cant access instade of sending him to homepage, so the code will be like that:我明白你在找什么,我有一个类似的项目,我认为你的代码缺少一个 if 语句来检查用户是否登录,我认为你最好留下一条消息来解释为什么用户无法访问 instade 发送给他到主页,所以代码将是这样的:

add_action( 'wp', 'hide_product_from_specific_role' );
function hide_product_from_specific_role() {
  if ( is_product() ) {
$product_id = get_the_ID();  
 if ( is_user_logged_in() && !current_user_can( 'specific_role' ) ) {
      wp_die( 'This product is not available to users with your role.' );
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM