Add product tags

This article explains how to automatically add tags to your WooCommerce products.

1

Identify fields to use for tags

First you need to identify the product fields you want to pull the tag values from. In this example, I will pull the tag values from 2 product fields: category & productgroup. You can see those fields when you view a product's details (by clicking the product's name):

Identify fields to use for tags
2

Create a custom plugin

If you haven't done so already, create a custom plugin.

3

Add custom code

Add the following code to your custom plugin file.

/**
 * Adds specific product fields as product tags.
 */
add_filter( 'dfrpswc_filter_taxonomy_array', 'mycode_add_tags', 20, 5 );
function mycode_add_tags( $taxonomies, $post, $product, $set, $action ) {

	$fields = array( 'category', 'productgroup' );
	$search = array( ";", ">", "," );

	$tags    = array();
	$replace = "||";
	foreach ( $fields as $field ) {
		if ( ! isset( $product[ $field ] ) ) {
			continue;
		}
		$words = str_replace( $search, $replace, $product[ $field ] );
		$words = explode( $replace, $words );
		foreach ( $words as $word ) {
			$tags[] = trim( $word );
		}
	}
	$taxonomies['product_tag'] = array_unique( $tags );

	return $taxonomies;
}
	
4

Modify code

The $fields array (line #7) allows you to set which fields should be used as tags. Notice that these are the exact fields found in the product details (see image above).

The $search array (line #8) allows you configure the delimiters used for each field. Some fields have multiple values separated by various characters such as ; and , and >.

5

Add/Update product sets

These changes will only take effect after any new Product Sets are created or after existing Product Sets are updated.

Still need help? Contact Us Contact Us