Pre-define the product tag according to the user nickname

We're shifting our Forum based support to a more dedicated support system!

We'll be closing our Forum support from 10th June, 2020 and move to Email Support assistance.

  • If you are a WCFM premium add-ons user, contact us- here
  • Want to know more before buying our add-ons? Send Pre sale queries- here
  • If you are a WCFM free user, please open a support ticket at WordPress.org
  • For WCFM App related queries, reach us- here
From now the forum will be read-only!

Multi Vendor Marketplace Plugin | WCFM Marketplace Forums WCFM – Ultimate Pre-define the product tag according to the user nickname

Viewing 7 reply threads
  • Author
    Posts
    • #102326
      paattal
      Participant

      Hello,

      I would like to predefine the tag of a product at creation / update.

      It would have to be equal to the user nickname and the tag field should not be visible so that the vendor cannot modify it.

      This tag allows me to filter products by vendors in other parts of the site by third-party plugins.

      Thank you

    • #103625
      paattal
      Participant

      Do you have a solution please?

    • #106112

      Hello,

      add_filter('wcfm_pm_product_tags_after_save','fn_wcfm_pm_product_tags_after_save',10,2);
      function fn_wcfm_pm_product_tags_after_save($product_tag_list, $product_id) {
      	$product_tag_list = explode(",",$product_tag_list);	
      	$vendor_tags = get_option('vendor_tags');
      	if(!empty($vendor_tags)) {
      		foreach ($vendor_tags as $vendor_tag) {
      			$term = get_term( $vendor_tag, 'product_tag' );
      			$del_val = $term->slug;
      			if (($searchkey = array_search($del_val, $product_tag_list)) !== false) {
      			    unset($product_tag_list[$searchkey]);
      			}
      		}
      	}	
      	return implode(',', $product_tag_list);
      }
      add_filter('tag_cloud_sort','fn_wcfm_tag_cloud_filter',10,2);
      function fn_wcfm_tag_cloud_filter($tags, $args) {
      	$vendor_tags = get_option('vendor_tags');	
      	if(!empty($vendor_tags)) {
      		foreach ($tags as $tag_key => $tag) {			
      			foreach ($vendor_tags as $vendor_tag) {				
      				if($tag->term_id == $vendor_tag) {					
      					unset($tags[$tag_key]);
      				}
      			}			
      		}
      	}	
      	return $tags;
      }
      add_action( 'after_wcfm_products_manage_meta_save', 'fn_custom_product_tag_save',10,2 );
      function fn_custom_product_tag_save($new_product_id, $wcfm_products_manage_form_data) {
      	$userid = get_current_user_id();
      	$vendor_tags = get_option('vendor_tags');
      	if(empty($vendor_tags)) {
      		$vendor_tags = array();
      	}
      	
      	if(wcfm_is_vendor($userid)) {
      		$current_user = wp_get_current_user();
      		$tag_name =$current_user->data->user_nicename;
      		$term = term_exists( $tag_name, 'product_tag' );		
      		if ( $term == 0 && empty($term) ) {
      		    $term_obj = wp_insert_term(
      			    $tag_name,   // the term 
      			    'product_tag', // the taxonomy
      			    array(
      			        'slug'        => $tag_name
      			    )
      			);
      		    $vendor_tags[] = $term_obj['term_id'];
      		    update_option('vendor_tags', $vendor_tags);
      		  	wp_set_object_terms( $new_product_id, $tag_name, 'product_tag', true );
      		} else {
      				$term_id = $term['term_id'];
      				wp_set_object_terms( $new_product_id, $tag_name, 'product_tag', true );
      		}
      	}
      }

      Try this code to your child theme’s functions.php
      In case you do not have child theme then add code using this plugin – https://wordpress.org/plugins/code-snippets/

      Thanks.

    • #106129
      paattal
      Participant

      Hello,

      Works fine thank you ! 🙂

      I just noticed that I forgot to specify that this field should be hidden for vendors (because autofill) and only appear for the admin (manual entry), sorry…

      • #106477

        Hello,

        “I just noticed that I forgot to specify that this field should be hidden for vendors (because autofill) and only appear for the admin (manual entry)”
        >>Can you please give me some clarification on this-
        1. Where do you need the Tag field (Admin user) in the “Add Product” page or in Vendor Profile page(Admin dashboard >> Store Vendors >> [[Selected Vendor]])?
        2. Can a vendor be associated with more than one tag? One automated (the code we shared), and one custom value set by the Admin. Or will it be a single value? i.e. when Admin sets a tag value for the vendor, it will override the automatic tag assignment (and delete the previously existing tag).

        Thanks.

    • #106519
      paattal
      Participant

      Hello,

      The code you shared is perfect for me because tag is automated for vendor (dashboard) and admin can add more tag.

      But vendor can not add or modify those tags, so tags field must be hidden for vendor in the “Add Product”.

      I need something like this but with the correct code :

      
      if(!current_user_can('administrator')) {
            add_filter('wcfm_is_allow_tags','__return_false');
         }
      

      Thanks

    • #106684

      Hello,

      What we understand that “vendor-nickname” tag only hide for vendor not for admin and hide “tag” field for vendor. Then modify below script,

      add_filter('wcfm_pm_product_tags_after_save','fn_wcfm_pm_product_tags_after_save',10,2);
      function fn_wcfm_pm_product_tags_after_save($product_tag_list, $product_id) {
      	$product_tag_list = explode(",",$product_tag_list);	
      	$vendor_tags = get_option('vendor_tags');
      	$userid = get_current_user_id();	
      	if(wcfm_is_vendor($userid)) {	
      		if(!empty($vendor_tags)) {
      			foreach ($vendor_tags as $vendor_tag) {
      				$term = get_term( $vendor_tag, 'product_tag' );
      				$del_val = $term->slug;
      				if (($searchkey = array_search($del_val, $product_tag_list)) !== false) {
      				    unset($product_tag_list[$searchkey]);
      				}
      			}
      		}
      	}	
      	return implode(',', $product_tag_list);
      }
      add_filter('tag_cloud_sort','fn_wcfm_tag_cloud_filter',10,2);
      function fn_wcfm_tag_cloud_filter($tags, $args) {
      	$vendor_tags = get_option('vendor_tags');
      	$userid = get_current_user_id();	
      	if(wcfm_is_vendor($userid)) {	
      		if(!empty($vendor_tags)) {
      			foreach ($tags as $tag_key => $tag) {			
      				foreach ($vendor_tags as $vendor_tag) {				
      					if($tag->term_id == $vendor_tag) {					
      						unset($tags[$tag_key]);
      					}
      				}			
      			}
      		}	
      	}
      	return $tags;
      }
      add_action( 'after_wcfm_products_manage_meta_save', 'fn_custom_product_tag_save',10,2 );
      function fn_custom_product_tag_save($new_product_id, $wcfm_products_manage_form_data) {
      	$userid = get_current_user_id();
      	$vendor_tags = get_option('vendor_tags');
      	if(empty($vendor_tags)) {
      		$vendor_tags = array();
      	}
      	if(wcfm_is_vendor($userid)) {
      		$current_user = wp_get_current_user();
      		$tag_name =$current_user->data->user_nicename;
      		$term = term_exists( $tag_name, 'product_tag' );		
      		if ( $term == 0 && empty($term) ) {
      		    $term_obj = wp_insert_term(
      			    $tag_name,   // the term 
      			    'product_tag', // the taxonomy
      			    array(
      			        'slug'        => $tag_name
      			    )
      			);
      		    $vendor_tags[] = $term_obj['term_id'];
      		    update_option('vendor_tags', $vendor_tags);
      		  	wp_set_object_terms( $new_product_id, $tag_name, 'product_tag', true );
      		} else {
      				$term_id = $term['term_id'];
      				wp_set_object_terms( $new_product_id, $tag_name, 'product_tag', true );
      		}
      	}
      }
      add_filter('wcfm_is_allow_tags',function($bool){
      	$userid = get_current_user_id();	
      	if(wcfm_is_vendor($userid)) {	
      		return false;
      	}
      	return $bool;
      });

      Please correct me if I am wrong.

      Thanks.

    • #106720
      paattal
      Participant

      Hello,

      This is exactly what I needed, thank you again for listening and your availability! 🙂

    • #106794

      You are always welcome 🙂

      Let me know if there’s anything else we can help you with.
      Can we ask for a favor? Would you mind taking a few minutes to review our plugin at https://wordpress.org/support/plugin/wc-multivendor-marketplace/reviews/ and let others know about your 5 Star experience with WCFM Marketplace. Also, follow us on Twitter https://twitter.com/wcfmmp for more exciting news, important updates, and irresistible offers.

Viewing 7 reply threads
  • The topic ‘Pre-define the product tag according to the user nickname’ is closed to new replies.