Add a Custom Taxonomy to Vendor Products

Let's add a custom taxonomy to products, for example a 'Brand' taxonomy, so that vendors can choose which brand each product belongs to.

Step 1: Create a taxonomy

First of all, we need to create the taxonomy itself if it doesn't already exist.

We can install the CPT UI plugin:

Then go to CPT UI -> Add/ Edit Taxonomies and create a new taxonomy with these settings:

Then in Products -> Brands we create a few sample options:

Step 2: Add the taxonomy to the vendor dashboard

Add this PHP code snippet to your site:

add_action('marketking_edit_product_after_tags', function($post){
	if (isset($post->ID)){
		<div class="row">
			<div class="col-xxl-3 col-md-6 marketking_card_gal_cat_tags">
			    <div class="code-block marketking_cattag_card">
			        <h6 class="overline-title title">
			            <?php esc_html_e('Brands','marketking-multivendor-marketplace-for-woocommerce');?>
			        <div class="form-group">
			            <div class="form-control-wrap">
			                $selected_brands = wp_get_object_terms($post->ID, 'brand', array('fields' => 'ids', 'hide_empty' => false));
			                $brand_args = array(
			                    'taxonomy' => 'brand',
			                    'name'     => 'marketking_select_brand',
			                    'class'    => 'form-select',
			                    'orderby'  => 'name',
			                    'title_li' => '',
			                    'multiple' => 'multiple',
			                    'hide_empty' => false,
			                    'selected' => implode(',',$selected_brands),
}, 10, 1);

// save taxonomy
add_action('marketking_after_save_product', function($product_id, $vendor_id){
    // Save "brand" taxonomy
    $brand_values = isset($_POST['marketking_select_brand']) ? $_POST['marketking_select_brand'] : array();
    foreach ($brand_values as $index => $val){
        $brand_values[$index] = intval($val); // transform to INT
    wp_set_object_terms( $product_id, $brand_values, 'brand' );

}, 10, 2);

This code can be added to functions.php of the child theme, or to any snippets plugin.


Vendors can now set brands for each product (one or multiple per product):

Brands on the vendor dashboard

Powered by BetterDocs