I want to create custom taxonomy for WooCommerce. The default taxonomy is category and tag, however, I want more, for example, brands, materials and more. For instance, I want Nike, Adidas... to be under brands; leather, canvas... to be under materials. When I search online, most tutorials are using plugins, like Advanced Custom Fields, to create custom taxonomy for WooCommerce. However, I really don't want to use plugin for just one purpose. I found this online: https://www.wppagebuilders.com/create-custom-taxonomy-without-plugin/ however it is for creating taxonomy for custom post type. I instead want to create custom taxonomy for WooCommerce. Therefore, the code doesn't work for me.
add_action('init', 'register_custom_taxonomy');
function register_custom_taxonomy()
{
$labels = array(
'name' => _x('Collections', 'taxonomy general name'),
'singular_name' => _x('Collection','taxonomy singular name'),
'search_items' => __('Search Collection'),
'all_items' => __('All Collection'),
'parent_item' => __('Parent Collection'),
'parent_item_colon' => __('Parent Collection:'),
'edit_item' => __('Edit Collection'),
'update_item' => __('Update Collection'),
'add_new_item' => __('Add New Collection'),
'new_item_name' => __('New Collection Name'),
'menu_name' => __('Collections'),
);
$args = array(
'hierarchical' => true, // make it hierarchical (like categories)
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'show_in_rest' => true,
'rewrite' => array('slug' => 'Collection'),
);
register_taxonomy('Collections', 'wpp_shoe', $args); // Register the Taxonomy
}
Anybody has solution?
To make your custom taxonomy linked to WooCommerce products, Some small adjustments are needed in your code (mainly to define "product" post type as the 2nd argument in register_taxonomy() function):
Code goes on functions.php file of your child theme (or in a plugin). Tested and works.