Easy to add Vendor category on WCFM Dashboard – Show on store page

Table of Contents

All data or code collected from WCFM official support team. Also, code tested.

The code worked fine and now the vendor “categories” are stored in the database in the user meta.

We’ve added a custom field so that the vendors can choose their category but it’s not quite what we are looking for, as we can’t use this field as a search filter.
For example, we want to have vendor categories such as electronics, clothes, accessories, etc (if our vendors sell products) or specialties (like dentists, pathologists, photographers, etc) if they offer services. From that categories we want them to make their choice during the membership steps, so we can later query our vendors by their category.
We’ve tried with the Vendors Group but it is not what we want, because we can only assign one group to our subscription plans, but we need more than one group per plan.

1st method:

Please try this code –

add_action( 'end_wcfm_membership_registration_form', function() {
	$selected_categories = array();
	
	$product_categories   = get_terms( 'product_cat', 'orderby=name&hide_empty=0&parent=0' );
	$p_category_options   = array();
	foreach ( $product_categories as $cat ) {
		$p_category_options[$cat->term_id] = array( 'name' => $cat->name, 'parent' => 0 );
	}
	
	if( !empty( $p_category_options ) ) showRegistrationCategoryHierarchySelector( $p_category_options, $selected_categories );
	?>
	<script>
	jQuery(document).ready(function($) {
		$('.wcfm_category_hierarchy').each(function() {
			$(this).change(function() {
				$level = parseInt($(this).data('level'));
				$value = $(this).val();
				if( $('.p_category_'+($level+1)).length > 0 ) {
					$('.p_category_'+($level+1)).addClass('wcfm_custom_hide');
					$('.p_category_label_'+($level+1)).addClass('wcfm_custom_hide');
					if( $value ) {
						$.each($value, function( $i, $svalue ) {
							if( $('.p_category_'+($level+1)).find('.cat_parent_'+$svalue).length > 0 ) {
								$('.p_category_'+($level+1)).removeClass('wcfm_custom_hide');
								$('.p_category_label_'+($level+1)).removeClass('wcfm_custom_hide');
							}
						});
					}
					$level_1 = $('.p_category_'+($level+1)).val();
					$('.p_category_'+($level+1)).val('');
					$('.p_category_'+($level+1)).find('option').addClass('wcfm_custom_hide');
					if( $value ) {
						$.each($value, function( $i, $svalue ) {
							$('.p_category_'+($level+1)).find('.cat_parent_'+$svalue).removeClass('wcfm_custom_hide');
						});
					}
					if( $level_1 ) {
						if( !$('.p_category_'+($level+1)).find('option[value="'+$level_1+'"]').hasClass('wcfm_custom_hide') ) {
							$('.p_category_'+($level+1)).val($level_1);
						}
					}
					$('.p_category_'+($level+1)).change();
				}
			}).change();
			
		});
	});
	</script>
	<?php
});

function showRegistrationCategoryHierarchySelector( $p_category_options, $selected_categories, $level = 0 ) {
	global $WCFM, $WCFMch;
	
	$p_category_child_options = array();
	$ptax_custom_arrtibutes = apply_filters( 'wcfm_taxonomy_custom_attributes', array(), 'product_cat' );
	?>
	<p class="wcfm_title p_category_label_<?php echo $level; ?>">
		<strong>
			<?php 
			echo apply_filters( 'wcfm_taxonomy_custom_label', '', 'product_cat' );
			if( $level ) _e( 'Sub-', 'wcfm-category-hierarchy' ); 
			_e( 'Categories', 'wc-frontend-manager' ); 
			?>
		</strong>
	</p>
	<label class="screen-reader-text" for="product_cats">
		<?php 
		if( $level ) echo __( 'Sub-', 'wcfm-category-hierarchy' );
		echo apply_filters( 'wcfm_taxonomy_custom_label', __( 'Categories', 'wc-frontend-manager' ), 'product_cat' ); 
		?>
	</label>
	<select id="p_category_<?php echo $level; ?>" style="height: auto;" multiple name="reg_category[<?php echo $level; ?>][]" class="wcfm-select wcfm_category_hierarchy p_category_<?php echo $level; ?>" data-level="<?php echo $level; ?>" data-catlimit="1" <?php echo implode( ' ', $ptax_custom_arrtibutes ); ?>>
		<option value=""><?php _e( '-- Select Category --', 'wcfm-category-hierarchy' ); ?></option>
		<?php
		foreach( $p_category_options as $term_id => $term_details ) {
			$cat_group_class = 'cat_parent_' . $term_details['parent'];
			echo '<option class="' . $cat_group_class . '" value="' . esc_attr( $term_id ) . '"' . selected( in_array( $term_id, $selected_categories ), true, false ) . '>' . __( esc_html( $term_details['name'] ), 'wcfm-category-hierarchy' ) . '</option>';
			
			$product_child_taxonomies   = get_terms( 'product_cat', 'orderby=name&hide_empty=0&parent=' . absint( $term_id ) );
			if ( $product_child_taxonomies ) {
				foreach ( $product_child_taxonomies as $cat ) {
					$p_category_child_options[$cat->term_id] = array( 'name' => $cat->name, 'parent' => $term_id );
				}
			}
		}
		?>
	</select>
	<?php
	$level++;
	if( !empty( $p_category_child_options ) ) showRegistrationCategoryHierarchySelector( $p_category_child_options, $selected_categories, $level );
}

add_action( 'wcfm_membership_registration', function( $member_id, $wcfm_membership_registration_form_data ) {
	if( isset( $wcfm_membership_registration_form_data['reg_category'] ) ) {
		update_user_meta( $member_id, 'wcfm_vendor_reg_category', $wcfm_membership_registration_form_data['reg_category'] );
	}
}, 50, 2 );

Implementation

Add code on your child theme function.php file. 

or add via the plugin

Advertisements
  1. Copy the code:
  2. Add new Code snippet in the Code Snippets plugin
  3. Paste the code snippet.
  4. Activate it.

 

2nd method:

 

Enable these features via plugin :

Download Plugin: here

 

“making it searchable with the sidebar in store listing Page”
– WCFM Marketplace already has this feature. You have to add this widget to the store list page sidebar – https://ibb.co/dPyV097

 

 

Another post: 

 

 

Check out our categories: Android Update, Elementor, flutter, Guide, Game, Plugin, Theme, webmaster, SEO, Woocommerce, WCFM

 

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

My Orders

My Downloads

My Booked Services

My Subscriptions