0

I'm setting up a multi-vendor ecommerce site using Woocommerce and Dokan Plugins. Only agricultural products will be sold on the site by me and other vendors.

However i am facing difficulties ajusting things to match my needs.

  • I want to add a private product category only available to me (not available/visible to other vendors) whenever i am creating a new product. For exemple When i (as the website admin) create a new product i can see/assign the product to the private category. But when other vendors are creating new products, they sould not be able to see/select the private category.

However in all cases, products added by admin and vendors should be visible on the website.

Can someone help me do the above please?

Kind see images below for clear explanations.

Thanks in advance

I am using woocommerce Version 4.2.0 and Dokan Version 3.0.5

Image 1: Admin dashboard when creating a new product Admin dashboard when creating a new product

Image 2: Vendor dashboard when creating new product Vendor dashboard when creating new product

ezybusy
  • 29
  • 8
  • The solution on the following link goes in the same line of idea as my question: [link]https://stackoverflow.com/questions/52635732/disable-some-product-categories-from-woocommerce-category-dropdown-widget [/link] Howevere in my case i want a specific product category to be hidden to Other Vendors (except admin) when they are adding new products. Maybe the restriction could be based on User Role or something else... – ezybusy Jun 27 '20 at 09:47

1 Answers1

0

Yes, it is possible:

With the pre_get_posts WordPress hook that is called after the query variable object is created, but before the actual query is run. So it is perfect for this case. We imagine here that the ID of 'administrator' category is '123'.

Here is the custom code:

function administrator_role_cat( $query ) {

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

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

        // Displaying All products (except "administrator" category products) 
        // to all other users roles (except "administrator" user role)
        // and to non logged user.
        } else {
            // Set here the ID for administrator category (with minus sign before)
            $query->set( 'cat', '-123' ); // negative number
        }
    }
}
add_action( 'pre_get_posts', 'administrator_role_cat' );
  • Hi @Khalid, thanks for your help. I changed the cat ID to **196** (which is the ID of my private category) and pasted the code in the **function.php** file of the theme i am using. But nothing changed. The private category is still visible to vendors and admin roles wehenever they try to create new products. – ezybusy Jun 27 '20 at 18:59
  • Maybe i did'nt give better explanations of what i am looking to achieve. Kindly see images added to the original question. – ezybusy Jun 27 '20 at 19:14
  • impossible any user, not administrator see this cat ID (123), Because you are in if statement –  Jun 28 '20 at 05:14
  • so appart from the if statement, what other work around can be used? – ezybusy Jun 28 '20 at 11:40