• Resolved hebhansen

    (@hebhansen)


    Hey Woo

    I have a number of product categories that admin, editor, store vendors shop managers etc. can tag into.

    I also have a category that I only want admin to be able to check products into. Right now everyone has access to this category How do I restrict access to this category and remove this category based on user role, when adding a product?

    I do not want to hide products within the category

    I do want to be able to display this category on my site for all to see, but only administered by admin and editors.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @hebhansen,

    To restrict access to a specific product category based on user roles, you might need to use a third-party plugin, as WooCommerce by default, doesn’t offer such granularity in permissions.

    A plugin like User Role Editor or Members could help you customize user roles and their capabilities.

    I hope this helps! Please let us know how it goes or if you need further assistance.

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @hebhansen,

    I’m marking this topic as “resolved” due to recent inactivity. If more assistance is needed, feel free to post back here or open a new topic.

    Thanks!

    Thread Starter hebhansen

    (@hebhansen)

    I have marked as not resolved…..

    Ideas for what I want to do?

    I have a hero image on my frontpage. I want this to be the latest product addition as a featured image or tag’ed into a category that query pulls. This should be administered by admin only, so since I cannot restrict other users to not see this “Secret” category, how can it then be obtained?

    1. Pull a hero frame from a tagged/featured/selected product
    2. Have the ability to display these featured subjects filtered inside Woo.

    I am currently using cover block for txt inside hero and that works really well.

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @hebhansen,

    This seems a bit complicated and couldn’t be achieved using default block or settings. It would need some customization to address. Unfortunately, custom coding or customization is not something we can assist with directly.

    Therefore, if you need help with customization or custom development tasks, it’s best to ask for insight related to those on either the WooCommerce Advanced Facebook group or the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:

    I wish I could help more, but hopefully, this gets you going in the right direction to get some further insight/information.

    Thread Starter hebhansen

    (@hebhansen)

    Well

    with a script that hides a category on user role problem is fixed imo. I have tested this in functions.php:

    // Filter access to categories
    add_filter( 'woocommerce_product_query_tax_query', 'storeapps_hide_shop_categories_by_role' );
    function storeapps_hide_shop_categories_by_role( $tax_query = array() ) {
    $user = wp_get_current_user(); // Get the current user object.
    $blocked_user_roles = array( 'author', 'contributor', 'customer', 'disable vendor', 'subscriber', 'shop manager', 'store vendor', 'wholesale customer', 'seo editor', 'seo manager' ); // Define an array of blocked user roles.
    $hidden_categories = array( 'Curator’s Choice' ); // Define an array of hidden categories.
    
    if ( is_user_logged_in() && count( array_intersect( $blocked_user_roles, $user->roles ) ) === 0 ) {
    return $tax_query;
    }
    
    $tax_query[] = array(
    'taxonomy' => 'product_cat',
    'terms' => $hidden_categories,
    'field' => 'slug',
    'operator' => 'NOT IN',
    );
    
    return $tax_query; // Return the modified tax query.
    }

    But it does not fly out of the box.

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hey @hebhansen,

    I’m glad you were able to find a solution to your inquiry here and thanks for sharing it with the community too! ??

    However, I noticed that you are using slug as the field in your tax_query. Please ensure that the values in your $hidden_categories array are indeed the slugs of the categories you want to hide, not their names. Also, please verify that the user roles in your $blocked_user_roles array are correct.

    If this makes no difference, it’s best to ask for insight related to those on either the WooCommerce Advanced Facebook group or the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question.

    I wish I could help more, but hopefully, this gets you going in the right direction to get some further insight/information.

    Thread Starter hebhansen

    (@hebhansen)

    Where do I find user role slug, or I should not use slug for those?

    Might be easier with ID ?

    Afzal (woo-hc)

    (@afzalpansuvi)

    Hi @hebhansen,

    Thanks for getting back to us. I understand you’re looking for a way to find the user role slug or ID. In WordPress, user roles are typically referred to by their slugs, not IDs. The slug is a string that WordPress uses to identify each role. In your code, you’ve correctly used the slugs for each role (e.g., ‘author’, ‘contributor’, ‘customer’, etc.).

    If you’re unsure about the slug for a particular user role, you can use a plugin like User Role Editor. This plugin allows you to view and edit user roles, including their slugs. I noticed that your code is set to hide the ‘Curator’s Choice’ category from all users with the specified roles. If you want to restrict access to this category only when adding a product, you might need a different approach.

    Please note that writing or providing custom code is not within the scope of our support policy. For development and custom coding questions, it’s best to ask for insight related to those on either the WooCommerce Advanced Facebook group or the WooCommerce Community Slack. You can also seek help from the following:

    Let us know if there’s anything else we can help you with.

    Thread Starter hebhansen

    (@hebhansen)

    All I want to do is hide one category from product editor, so that vendors cannot add stuff to this category. I have an idea that someone else also need this eventually.

    Slug shop manager is then what? shop_manager / shop-manager / shopmanager ?

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @hebhansen,

    You’re right. In WordPress, slugs are typically lowercase and underscore (_) to separate words. Therefore, the slug for the Shop Manager is indeed “shop_manager”.

    As for your requirement to hide a specific category from vendors in the product editor, your current approach seems correct. However, please note that custom code development is beyond our scope of support.

    I hope this clarifies your concern. If you have any other questions, feel free to ask.

    Thread Starter hebhansen

    (@hebhansen)

    Hey Shameem

    Good to know and thx for sharing…. I have updated my snippet as specified and it still does not work. I will leave the snippet active as is and give some attention along the way.

    I understand custom code is outside Woo, however, I am stunned this is not core Woo, so maybe give it some consideration.

    I will mark as solved, though it’s not and see what comes along.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Check product into categories – limit by user role’ is closed to new replies.