Import brands for use with the WooCommerce Brands plugin

This code imports product brand names into the "product_brand" taxonomy if you are using the WooCommerce Brands plugin. Here's how to make it work.

Note

Brand names will only be imported AFTER your Product Sets update.

1

Create a custom plugin

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

2

Add custom code

Add the following code to your custom plugin.

/**
 * Adds the brand name to the 'product_brand' taxonomy.
 *
 * This will import the brands into the 'product_brand' taxonomy when using
 * the WooCommerce Brands plugin.
 *
 * @link http://www.woothemes.com/products/brands/
 *
 * @param array $taxonomies The currently configured array of taxonomies.
 * @param array $post An array of post data including ID, post_title, post_status, etc...
 * @param array $product An array of product data returned from the Datafeedr API.
 * @param array $set A post array for this Product Set with an array key of postmeta containing all post meta data.
 * @param string $action The action the Product Set is performing. Value are either "insert" or "update".
 *
 * @return array Updated $taxonomies array
 */
add_filter( 'dfrpswc_filter_taxonomy_array', function ( $taxonomies, $post, $product, $set, $action ) {

   $taxonomy = 'product_brand';
   $brand    = isset( $product['brand'] ) ? trim( $product['brand'] ) : false;

   if ( ! $brand ) {
      return $taxonomies;
   }

   if ( term_exists( $brand, $taxonomy ) === null ) {
      wp_insert_term( $brand, $taxonomy );
   }

   $taxonomies[ $taxonomy ] = $brand;

   return $taxonomies;
   
}, 10, 5 );
3

Save

Save your changes.

4

Delete Brand attribute (optional)

Lastly, since you don't need the "Brand" attribute anymore, go here WordPress Admin Area > Products > Attributes and delete the "Brand" attribute from that page.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us