Introduction When running an eCommerce site using WooCommerce, the cart page plays a vital role…
How to Create and Display a Custom Brand Taxonomy in WooCommerce
Overview:
In this guide, we explain how to create a custom brand taxonomy in WooCommerce, allowing you to group products by brands. We cover everything from registering the taxonomy, adding brand terms, and assigning them to products, to displaying the brands on the product pages. This process improves your store’s organization and enhances the shopping experience. We also show you how to style the brand display for a polished look.
What is a Taxonomy in WordPress?
In WordPress, a taxonomy is a way to group posts or custom post types. In WooCommerce, you can create a custom taxonomy to organize products by their brands, which can significantly enhance your ecommerce taxonomy strategy.
Here’s how to set it up.
Step 1: Register the Custom Brand Taxonomy
First, add the following code to your theme’s functions.php
file or a custom plugin to create your WooCommerce custom taxonomy:
// Register the custom taxonomy
add_action('init', 'create_product_brand_taxonomy', 0);
function create_product_brand_taxonomy() {
$labels = array(
'name' => _x('Brands', 'taxonomy general name', 'textdomain'),
'singular_name' => _x('Brand', 'taxonomy singular name', 'textdomain'),
'search_items' => __('Search Brands', 'textdomain'),
'all_items' => __('All Brands', 'textdomain'),
'edit_item' => __('Edit Brand', 'textdomain'),
'add_new_item' => __('Add New Brand', 'textdomain'),
'menu_name' => __('Brands', 'textdomain'),
);
$args = array(
'hierarchical' => true, // True for categories, false for tags
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'brand'),
);
register_taxonomy('product_brand', array('product'), $args);
}
Step 2: Add Brand Terms
Once your custom taxonomy is registered:
- Go to your WordPress dashboard.
- Navigate to Products > Brands.
- Add new brands just like you would add categories or tags.
This step helps you build a solid woocommerce branding strategy.
Step 3: Assign Brands to Products
To assign brands to products:
- Open any product for editing or create a new one.
- In the product edit screen, you’ll find a “Brands” box where you can assign existing brands or add new ones. This is essential for creating a streamlined woocommerce add brand to product process.
Step 4: Display Brands on Product Pages
Now, to show the brand(s) on the product page, add the following code to your theme’s functions.php
file or a custom plugin:
// Display brands on the product page
add_action('woocommerce_single_product_summary', 'display_product_brands', 25);
function display_product_brands() {
global $product;
$terms = wp_get_post_terms($product->get_id(), 'product_brand');
if ($terms && ! is_wp_error($terms)) {
$brands = array();
foreach ($terms as $term) {
$brands[] = $term->name;
}
echo '<div class="product-brands">' . __('Brands: ', 'textdomain') . implode(', ', $brands) . '</div>';
}
}
Step 5: Style the Brands Display
You can customize how the brand names appear on the product page by adding this CSS to your theme’s style.css
file:
.product-brands {
font-size: 14px;
margin-top: 10px;
}
.product-brands span {
font-weight: bold;
}
Conclusion
With these simple steps, you can easily create and display a custom brand taxonomy in WooCommerce. This will help customers browse your products by brand, improving their shopping experience and making your site more organized. Implementing a custom product builder for WooCommerce can also further enhance your offerings.
FAQ
What is a taxonomy in WooCommerce?
It’s a way to group products by categories, such as brands.
How can I create a custom brand taxonomy?
Add a code snippet to your theme’s functions.php
file or a custom plugin.
How do I display brands on product pages?
Use a code snippet to show brands on the product page.
Can I rename the taxonomy from ‘Brand’ to something else?
Yes, you can customize the labels in the code to fit any term, like ‘Manufacturer’ or ‘Designer.’
Is this taxonomy searchable on my store?
Yes, the brand taxonomy will be searchable if your theme supports it.
This Post Has 0 Comments