• Resolved NicheLabs

    (@allennichelabs)


    Is it possible to create product categories that are only available to specific vendors?

    I’ve been working with the free version of WC Vendors and have a few vendors setup. I’m trying to setup sections that only show categories and products for the specific vendor. I’ve done this for now by creating parent categories with the vendor’s name and on their shop page using a short code to display only those categories.

    The problem I ran across is when a vendor goes to add or edit a product, they have access to all categories. So they can easily create a product and assign it to a category that I’ve created for a different vendor. This will cause their product to show up in another vendor’s listings.

    This isn’t good since all the vendors are selling similar products and this could easily allow them to steal customers from other vendors.

    I need to be able to create categories that only vendor xxx can see/use and other categories that only vendor yyy can see/use.

    Is this possible with the pro version?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Jamie

    (@digitalchild)

    Hello,

    This is not possible with how WordPress is built. There is no capability restrictions/users restrictions you an put on taxonomies.

    On a different note, I would look into how to better structure your site if vendors can ‘steal’ customers from each other the marketplace is going to have serious issues in the long run.

    cheers,

    Jamie.

    NicheLabs

    (@allennichelabs)

    I’m not sure how to “better structure” my site. That’s kind of the whole problem. Since I can’t restrict categories to a specific vendor how can I keep other vendors products from showing up on another vendors store?

    All vendors can see and therefor add products to all categories. Without being able to restrict products to a specific vendor in the vendor’s store I completely agree that the marketplace is going to have serious issues. That’s why I’m posting here.

    I can’t list the products individually for each vendor, that’s just not feasible. I need to be able to create individual stores where each vendor can list their products, and only their products. There must be a way to do this.

    NicheLabs

    (@allennichelabs)

    Added note:

    Currently I’ve created categories that I only display on each vendor’s page by using Woocommerce’s shortcodes to display categories.

    So for Vendor A I have categories such as:
    Vendor A Category
    – Vendor A Subcategory 1
    – Vendor A Subcategory 2
    etc..

    I have this structure for each vendor. But the issue comes up because vendors are allowed to select any category when adding new products or editing existing products. So Vendor B can assign a product to Vendor A’s category, selling the same or similar item at a cheaper price and thus “stealing” the customer from Vendor A.

    Disclaimer: I’m not one of the developers of WC Vendors. If my code doesn’t work or something, don’t blame them.

    You would have to write some custom code to hide certain categories from certain vendors.

    WordPress offers the filter “get_terms_args”, which can be useful for situations like this. The filter runs on the product edit screen right before WordPress fetches a list of categories to display to the user.

    
    function wcv_categories_restricted_to_vendors($args) {
    	if (!is_admin()) return $args;
    	
    	$screen = get_current_screen();
    	if (!$screen instanceof WP_Screen || $screen->post_type !== 'product' || $screen->base !== 'post') return $args;
    	
    	$exclusions = array(
    		'1st_vendor_id_goes_here' => array(
    			'1st_product_category_id_goes_here',
    			'2nd_product_category_id_goes_here'
    		),
    		'2nd_vendor_id_goes_here' => array(
    			'1st_product_category_id_goes_here',
    			'2nd_product_category_id_goes_here'
    		)
    	);
    	
    	$user_id = get_current_user_id();
    	
    	if (class_exists('WCV_Vendors') && WCV_Vendors::is_vendor($user_id)) {
    		if (array_key_exists($user_id, $exclusions)) {
    			$args['exclude'] = $exclusions[$user_id];
    		}
    	}
    	
    	return $args;
    }
    add_filter('get_terms_args', 'wcv_categories_restricted_to_vendors');
    

    ^this would go in your theme’s functions.php file. This is a rough example which you should use as a starting point for developing a more robust solution. I briefly tested it using one of my vendors and it appears to be working as expected.

    Replace “1st_vendor_id_goes_here” with your vendor’s user id. Replace “1st_product_category_id_goes_here” with the ID of the product category you want to hide from that vendor. Add additional vendors/categories as needed. A category’s ID can be retrieved while editing a product category as it appears in your browser’s address bar.

    In my example above, the $exclusions array is written in the code, but ideally you’d want to generate that array through some other means. Perhaps as term meta on the product categories.

    Also, be aware that this code replaces any previous exclusion rules that may have been added elsewhere in your site. The exclusions it adds should ideally be added to any previous exclusions passed by the filter.

    More info about the filter get_terms_args can be found here:
    https://developer.wordpress.org/reference/classes/wp_term_query
    https://core.trac.wordpress.org/browser/tags/4.9/src/wp-includes/class-wp-term-query.php#L237

    NicheLabs

    (@allennichelabs)

    GoTeamScotch,

    I appreciate your help. I applied a similar solution but went a slightly different route.

    I added a field to the categories “Vendor ID”. Then I used the same filter you mentioned above but rather than using the ‘exclude’ argument I used the meta_key and meta_value arguments:

    function nl_filterGetTermArgs($args, $taxonomies) {
        global $typenow;
    
        if ($typenow == 'product') { 
            // check whether we're currently filtering selected taxonomy
            if (implode('', $taxonomies) == 'product_cat' && is_edit_page()) {
    			$role = nl_get_user_role();
    			$vendorID = get_current_user_id();
    			if ($role == 'vendor') {
    				$args['meta_key'] = 'nl_vendor_id';
    				$args['meta_value'] = $vendorID;
    			}
            }
        }
    
        return $args;
    }

    This way the vendor’s id isn’t hardcoded and you can set any category to belong to any vendor without having to further modify your code. This also allows the administrator to still see all available categories.

    Plugin Author Jamie

    (@digitalchild)

    Thanks @goteamscotch ! Didn’t think about filtering for this. A nice solution!

    @allennichelabs be careful when using meta queries, they can have performance issues if you have large data sets.

    cheers,

    Jamie.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Categories Restricted To Vendors’ is closed to new replies.