Add Product Brand in WooCommerce

  • 8 comments
  • 3040 Views
  • Print

Even though you can use product category feature of WooCommerce to organize your products, sorting products by brands was not there by default. Yes, there are few plugins out there that can get your job done but how about doing it by yourself without using any plugin? This is exactly what I did on one of my project.

The idea is fairly simple. All you need to do is to register a custom taxonomy that supports “product” post type. This is as simple as it can get. I won’t spend time explaining what is happening on the following snippet as in one of my previous post, I discussed on how to register a taxonomy for a default posts or a custom post type. So, here is the snippet.

// register product_brand taxonomy for woocommerce	
function add_product_brands(){
   register_taxonomy('product_brand',array('product'), array(
      'labels' => array(
         'name' => 'Brands',
	 'singular_name' => 'Brand',
	 'search_items' => 'Search Brands',
	 'all_items' => 'All Brands',
	 'parent_item' => 'Parent Brand',
	 'parent_item_colon' => 'Parent Brand:',
	 'edit_item' => 'Edit Brand', 
	 'update_item' => 'Update Brand',
	 'add_new_item' => 'Add New Brand',
	 'new_item_name' => 'New Brand',
	 'not_found' => 'No Brand Found',
	 'menu_name' => 'Brands'),
      'hierarchical' => true,
      'query_var' => true,
      'public' => true,
      'show_tagcloud' => true,
      'show_admin_column' => true,
      'show_in_nav_menus' => true,
      'sort' => '',
      'rewrite' => array('slug' => 'brand','with_front' => false),
      'show_ui' => true)); }
add_action('init','add_product_brands', 0);

Add this snippet on your current theme’s functions.php page and update it. Now you should see the “Brands” text sub menu item underneath the “Products” menu item.
Add Brands to WooCommerce
Now, you are ready to add as many brand as you want and associate them with your products.
Brand Selection Widget on SidebarAt this point simply go ahead and try to add a product as you would used to do normally. Take a careful look on the right hand side of the screen. Right above the “Categories” (Product) widget, you should see the “Brands” widget (see the screenshot) that would allow you to select the brand with your products.

We are half way done and got few more things to cover before you can fully take the advantage of our works so far. Let’s move on and see what else we need to do.

List the Brands

Without displaying the list of Brands and their associated products, the whole effort would be useless. So, let’s display the list of Brands that has products associated with them. You should use the following snippet, wherever you think this list should be displayed.

<?php 
// get all the brands with link
$brands = get_terms(array(
   'taxonomy' => 'product_brand',
   'hide_empty' => true)); 
echo '<ul>';
   foreach ($brands as $brand) { 
      echo '<li><a href="'.get_term_link($brand->slug, $brand->taxonomy).'">'.$brand->name.'</a></li>'; 
   }
echo '</ul>';
?>

Generally speaking though, this snippet should belong to somewhere in your template page. This is your turn to think and figure out where you should display this list. Also go ahead and use your CSS magic to style them.

Single Product Page

Now, when your potential customers are looking at the product (single product page), you may want to display the name of the the brand. This is one way to provide more information about your product. Here is the little snippet that you can add on your theme’s functions.php page and display the name of your product’s brand. This link on the brand names will take your visitor to the archive page of this particular brand.

// add single product brand with link
add_action('woocommerce_product_meta_start','display_brand_on_product_page', 10);
function display_brand_on_product_page(){
	echo '<span class="posted_in">'.get_the_term_list($wp_query->post->ID,'product_brand','Brand: ','','','').'</span>';
}

You should be all set at this point.

I hope you would find this post to be very useful. Feel free to share this post and leave your questions(if any) from the comment section below. I will try to get back to you within the possible shortest time frame. Thank you.

Note: Little demonstration of these snippets can be witnessed on one of the eCommerce site that I built. Feel free to check out this page for better understanding.

Comment

8 Comments

  1. VirgilioPublished: 1 year ago

    Hello, thank you for the article. It worked great.
    But I have a question. I’m using ‘Product CSV Importer and Exporter’. How would you include this new brand taxonomy in the import/export file?

  2. JaredPublished: 1 year ago

    I was able to add the brands to my site but after adding the brands, I wanted to added collections. So I changed all words with Brands or brand for Collections or collection. I also input that in the functions.php file. Hoping you can tell me why it wouldn’t take. It actually took over for the brands code so I can’t have both codes in the functions.php file.

    • IftekharPublished: 1 year ago

      What I understand from your comment is that you want to have both the “Brand” and “Collection” taxonomy for your WooCommerce products. This is fairly easy and there are two methods how you can achieve that. I will show you the easies one. This should be useful.

      add_action('init','add_product_brand_and_collection', 0);
      function add_product_brand_and_collection(){
         register_taxonomy('product_brand','array('product')', array());
         register_taxonomy('product_collection','array('product')', array());
       }

      Hope that helps. Thank you.

  3. pacificPublished: 1 year ago

    Thank you for share.
    I made the step config functions.php file and got the display in admin page and & single product page.
    But “List the Brands” is not currently displayed. What file do you put that code into? I want it to show on the sidebar page of the categories page and single product page.
    Thank you!

    • IftekharPublished: 1 year ago

      That should be on your “sidebar.php” page even though you can literally put that snippet anywhere you want. Hope that helps. Thanks.

  4. AleksandarPublished: 6 months ago

    Hi,

    The code works great.

    How can I add the brand link in the product page before title (or after title).

    Thanks

    • IftekharPublished: 6 months ago

      Simply add the following snippet wherever (within the loop) you want to display the brand of your product.

      <?php echo get_the_term_list($wp_query->post->ID,'product_brand','Brand: ','','',''); ?>

      Hope that helps. Thank you.

  5. KarinePublished: 3 months ago

    Hello,

    Thanks for the code, it helped me a lot!
    While I’m on debug mode I get some errors :
    Notice: Undefined variable: wp_query
    Notice: Trying to get property ‘post’ of non-object
    Notice: Trying to get property ‘ID’ of non-object.
    But the brand still shows on the page… How can I make this right?

    Thanks again

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from Postable, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.