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.
Now, you are ready to add as many brand as you want and associate them with your products.
At 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 that would allow you to select the brand associated 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(){
global $product;
echo '<span class="posted_in">'.get_the_term_list($product->get_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.
Comments
Commenting is disabled.