Create a Custom Taxonomy: WooCommerce allows you to create custom taxonomies to categorize your products. We can utilize a custom taxonomy to represent the "Brand" for each product. Add the following code to your theme's functions.php file or create a custom plugin:
// Register 'Brand' custom taxonomy
function custom_register_brand_taxonomy() {
$labels = array(
'name' => _x( 'Brands', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Brand', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Brands', 'textdomain' ),
'popular_items' => __( 'Popular Brands', 'textdomain' ),
'all_items' => __( 'All Brands', 'textdomain' ),
'edit_item' => __( 'Edit Brand', 'textdomain' ),
'update_item' => __( 'Update Brand', 'textdomain' ),
'add_new_item' => __( 'Add New Brand', 'textdomain' ),
'new_item_name' => __( 'New Brand Name', 'textdomain' ),
'separate_items_with_commas' => __( 'Separate brands with commas', 'textdomain' ),
'add_or_remove_items' => __( 'Add or remove brands', 'textdomain' ),
'choose_from_most_used' => __( 'Choose from the most used brands', 'textdomain' ),
'not_found' => __( 'No brands found', 'textdomain' ),
'menu_name' => __( 'Brands', 'textdomain' ),
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'brand' ),
);
register_taxonomy( 'brand', 'product', $args );
}
add_action( 'init', 'custom_register_brand_taxonomy', 0 );
Edit Products: After adding the custom taxonomy, you can now assign brands to your products.
Edit each product in WooCommerce.
In the Product Data section, you should now see a new option for "Brands" or "Brand" depending on the labels you set in the code.
Select or enter the brand for the respective product.
Save the changes to update the product's brand information.
Display Brand on Product Pages: To display the brand on the frontend, you will need to modify your theme templates. Locate the appropriate template file (e.g., single-product.php or content-product.php) and add the following code where you want to display the brand name:
This code retrieves the brand associated with the product and displays it on the product page.
<?php
$brands = get_the_terms( get_the_ID(), 'brand' );
if ( $brands && ! is_wp_error( $brands ) ) {
foreach ( $brands as $brand ) {
echo '<span class="product-brand">' . $brand->name . '</span>';
}
}
?>
By creating a custom taxonomy and assigning brands to each product, you can manually add brand information without using plugins.