Add the Vendor’s Product Categories to Vendors List Page

This snippet was provided by support to show a list of the product categories each individual vendor has for sale onto the Vendors List page.

Note: I do not use this snippet on my own site, so I don’t know if or how it works. Please let me know in the comments!

As always, this is either added to your Child Theme’s functions.php file, or consider using the My Custom Functions Pro plugin to manage and organize all your snippets.

add_action('wcmp_vendor_lists_vendor_after_store_details', 'show_vendor_categories');

function show_vendor_categories($vendor) {
	$vendor_products = $vendor->get_products(array('fields' => 'ids'));
	$product_categories = array();
	$i = 0;
	echo '<li><b>Categories</b><br>';
	foreach ($vendor_products as $product_id) {
		$product_category_list = wc_get_product_category_list($product_id, $sep='This is a separator');	//lists all categories the product is assigned to
		$product_category_array = explode ('This is a separator', $product_category_list);	//converts the list into an array splitting at the identifier 'This is a separator'
		$product_category = $product_category_array[count($product_category_array)-1];		//Only lowest category hierarchy level to be displayed if product is assigned to category + subcategory					
		if (!in_array($product_category, $product_categories)) {	//makes sure to add only categories that are not yet inside the array
			array_push($product_categories, $product_category);
			if ($i == 0) {
				echo $product_category;
			} else {
				echo ', '.$product_category;	//adds a comma if more than one category will be shown
	echo '</li>';

Comments on this post

Your email address will not be published. Required fields are marked *