Normalize brand names

This article explains how to normalize brand names.

Occasionally, a product will have different brand names. For example, a pair of shoes might have the brand name Nike whereas other pairs of shoes might have the brand name Nike, Inc..

By normalizing brand names, we can reduce brand name variations to one version.

Before

Before normalizing brand names

After

After normalizing brand names

Before you begin

Before brand names get imported into your WooCommerce store, you must first make sure you have added the attribute "Brand". To do this, go here WordPress Admin Area > Products > Attributes. Add the word Brand in the "Name" field and then click the [Add Attribute] button.

1

Create a custom plugin

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

2

Add custom code

Add the following custom code

/**
 * Normalize brand names.
 *
 * The attribute "Brand" with a slug of "brand" must already exist here:
 * WordPress Admin Area > Products > Attributes.
 */
add_filter( 'dfrpswc_filter_attribute_value', 'mycode_add_brand_attribute', 20, 6 );
function mycode_add_brand_attribute( $value, $attribute, $post, $product, $set, $action ) {
	if ( $attribute == 'pa_brand' ) {
		if ( isset( $product['brand'] ) ) {
			$value = mycode_get_brand( $product['brand'] );
		}
	}

	return $value;
}

/**
 * Normalize brand attribute names.
 *
 * This returns a normalized value of a term based on a supplied
 * array of mappings of a key (desired word) mapped to an array of
 * keywords (undesired words).
 */
function mycode_get_brand( $field, $default = '' ) {

	// Initialize $map array.
	$map = array();


	// ++++++++++ Begin Editing Here ++++++++++

	$map["Aerobie"]        = array( "Aerobie, Inc." );
	$map["Black Diamond"]  = array( "Black Diamond Equipment", "Black Diamond Inc" );
	$map["CAMP USA"]       = array( "camp", "camp usa - cassin" );
	$map["Crucial Coffee"] = array( "crucial brands", "crucial-coffee" );

	// ++++++++++ Stop Editing Here ++++++++++


	// Initialize the $terms array.
	$terms = array();

	// Loop through $map, searching for $field in $map key or in $map keywords.
	foreach ( $map as $key => $keywords ) {

		// Add $key to $terms array if the $map key matches the field.
		if ( preg_match( '/\b' . preg_quote( $key, '/' ) . '\b/i', $field ) ) {
			$terms[] = $key;
		}

		// Add $key to $terms array if $field exists in array of keywords.
		foreach ( $keywords as $keyword ) {
			if ( preg_match( '/\b' . preg_quote( $keyword, '/' ) . '\b/i', $field ) ) {
				$terms[] = $key;
			}
		}
	}

	// If there are $terms, implode with WC_DELIMITER as separator.
	if ( ! empty( $terms ) ) {
		return implode( WC_DELIMITER, array_unique( $terms ) );
	}

	// No matches were found in $map for $field so return the $default value.
	return $default;
}
	
3

Edit brand names

On lines 33~36 of the above code, you will find the code that needs to be edited for your purposes.

The value on the left (ex. $map["Black Diamond"]) is the brand name that you want to use.

The value on the right (ex. array( "Black Diamond Equipment", "Black Diamond Inc" ) is an array of values you want to replace with the value on the left. Each value in the array should be surrounded with double quotes "" and be comma separated.

4

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