• Resolved Dani

    (@danicasati)


    Hi.
    Is it possible to force Woocommerce to add automatically a product tag when vendor create a new product?
    Maybe, tag could be vendor’s slug.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Alessio Torrisi

    (@alessio91)

    Hi there,
    I did a custom code to help you achieve your goal.
    Please add following code in the file functions.php of your theme and let me know.

    add_action( 'save_post', 'yith_wcmv_create_tag_on_new_product' );
    
    if( ! function_exists( 'yith_wcmv_create_tag_on_new_product' ) ){
    
    	function yith_wcmv_create_tag_on_new_product( $post_id ){
    		$vendor = yith_get_vendor( $post_id, 'product' );
    	
    		if ( $vendor->is_valid() ) {
    			if( ! term_exists( $vendor->slug, 'product_tag' ) ){
    				wp_insert_term(
    					$vendor->name,   // the term 
    					'product_tag', // the taxonomy
    					array(
    						'slug'        => $vendor->slug,
    					)
    				);				
    			} 
    			wp_set_object_terms( $post_id , $vendor->slug, 'product_tag' );
    		}
    
    	}
    
    }

    Thank you for your time.

    Thread Starter Dani

    (@danicasati)

    Thanks for support.
    I’ll test it today.

    • This reply was modified 3 years, 6 months ago by Dani.
    Thread Starter Dani

    (@danicasati)

    It works, but the code is deleting all terms and only saves vendor’s slug.
    How can I keep exisiting tags and just add vendor’s slug to product tags list?

    Thread Starter Dani

    (@danicasati)

    I found it.

    Just replace:
    wp_set_object_terms( $post_id , $vendor->slug, 'product_tag' );

    with:
    wp_set_object_terms( $post_id , $vendor->slug, 'product_tag', $append = true );

    • This reply was modified 3 years, 6 months ago by Dani.
    Plugin Support Alessio Torrisi

    (@alessio91)

    Hi there,
    great, I’m happy to have helped you.
    If you’re happy with our product and our support, May I ask you leave a review ? ??
    We’ll be happy receive your feedback, thank you for your time.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Force product tag by vendor’ is closed to new replies.