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.

/**
 * @see https://datafeedrapi.helpscoutdocs.com/article/194-import-brands-for-use-with-the-woocommerce-brands-plugin
 */
add_action( 'dfrpswc_post_save_product', function ( Dfrpswc_Product_Update_Handler $updater ) {

   $taxonomy = 'product_brand';
   $brand    = isset( $updater->dfr_product['brand'] ) ? trim( $updater->dfr_product['brand'] ) : false;

   if ( ! $brand ) {
      return;
   }

   $term = term_exists( $brand, $taxonomy );

   if ( empty( $term ) ) {
      $term = wp_insert_term( $brand, $taxonomy );
   }

   if ( is_wp_error( $term ) ) {
      return;
   }

   if ( ! isset( $term['term_id'] ) ) {
      return;
   }

   wp_set_object_terms( $updater->post['ID'], absint( $term['term_id'] ), $taxonomy );
} );
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