Multi Vendor Marketplace Plugin | WCFM Marketplace › Forums › WCFM – Ultimate › Pre-define the product tag according to the user nickname
- This topic has 8 replies, 2 voices, and was last updated 4 years, 1 month ago by Sarmistha Chakraborty.
- AuthorPosts
- January 16, 2020 at 9:08 pm #102326paattalParticipant
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
- January 24, 2020 at 6:27 pm #103625paattalParticipant
Do you have a solution please?
- February 7, 2020 at 6:14 pm #106112Sarmistha ChakrabortyKeymaster
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.
- February 7, 2020 at 9:13 pm #106129paattalParticipant
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…
- February 10, 2020 at 4:03 pm #106477Sarmistha ChakrabortyKeymaster
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.
- February 10, 2020 at 8:11 pm #106519paattalParticipant
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
- February 11, 2020 at 3:26 pm #106684Sarmistha ChakrabortyKeymaster
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.
- February 11, 2020 at 7:36 pm #106720paattalParticipant
Hello,
This is exactly what I needed, thank you again for listening and your availability! 🙂
- February 12, 2020 at 11:20 am #106794Sarmistha ChakrabortyKeymaster
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.
- AuthorPosts
- The topic ‘Pre-define the product tag according to the user nickname’ is closed to new replies.