There are two types of taxonomies in WooCommerce: product taxonomies and customer taxonomies. Product taxonomies are used to group products together, while customer taxonomies are used to group customers together. In this article, we will focus on how to create a custom product taxonomy in WooCommerce.
Product taxonomies in WooCommerce are used to categorize and organize products. By default, WooCommerce comes with three built-in product taxonomies: product categories, product tags, and product shipping class. However, you can also create custom product taxonomies to better suit your needs.
Creating a custom product taxonomy in WooCommerce is a two-step process: first, you need to register the taxonomy, and then you need to add thetaxonomy to a product.
To register a custom taxonomy, you need to add the following code to your theme’s functions.php file:
function my_custom_taxonomy() {
$labels = array(
'name' => _x( 'My Custom Taxonomy',
'taxonomy general name',
'textdomain' ),
'singular_name' => _x( 'My Custom Taxonomy',
'taxonomy singular name',
'textdomain' ),
'search_items' => __( 'Search My Custom Taxonomies',
'textdomain' ),
'all_items' => __( 'All My Custom Taxonomies',
'textdomain' ),
'parent_item' => __( 'Parent My Custom Taxonomy',
'textdomain' ),
'parent_item_colon' => __( 'Parent My Custom Taxonomy:',
'textdomain' ),
'edit_item' => __( 'Edit My Custom Taxonomy',
'textdomain' ),
'update_item' => __( 'Update My Custom Taxonomy',
textdomain' ),
'separate_items_with_commas' => __( 'Separate my custom taxonomies with commas',
'textdomain'),
// New in 3.0! This line is not required in prior versions of
WordPress (3.0 or higher). It just gives you more options for how
your data will be displayed in the admin interface.
You can read more about this here:
http://codex.WordPress.org/Function_Reference/register_
taxonomy#Arguments add_or_remove_items => __(
'Add or remove my custom taxonomies', 'textdomain'),
choose_from_most_used => __( 'Choose from the most used my custom taxonomies',
'textdomain'),
not_found => __( "No my custom taxonomies found.",
"textdomain" ),
no_terms => __("No my custom taxonomIES",
"textdomain"), );
$args = array(
// This array of options controls the labels displayed in
the WordPress Admin UI
labels => $labels,
// A string value of either hierarchical or
non-hierarchical may be passed as name
// This string determines whether a giventaxonomy
is hierarchical (categories) or
non-hierarchical (tags) hierarchical => true,
// A boolean value that sets whether thetaxonomy should have a
flat structure (like tags) or a hierarchy (like categories)
show_ui => true,
// Whether to show the user interface for managing
this taxonomy show_in_menu => true,
// Whether to show this taxonomy in the WordPress Admin Menu
show_innavMenus => true,
// Whether this taxes should be publicly queryable
public => true,
// Whether this taxes is publicly editable
showTaxInQuickEdit => false,
// Whether to show this taxes in the quick/bulk edit screen
metaBoxesCallback function () use (&$postType){
if (! ($postType instanceof WPPostType)) {
return; } $screen = getCurrentScreen();
if ($screen && $screen->id === "edit-" .
$postType->name) {
removeMetaBox("my-custom-tax");
addMetaBox("my-custom-tax",
"My Custom Tax",
"renderTaxMetaBox",
$postType->name);
}
}, );
registerTax($args); }
addAction('init','myCustomTax');
function renderTaxMetaBox($postType){
global $wpdb; $terms = getTerms([ "tax"=>"my-custom-tax",
"hideEmpty"=>false ]);
foreach ($terms as &$term){
$term->count = intval($wpdb->getVar(
$wpdb->prepare("SELECT COUNT('*') FROM {
$wpdb->termRelationships} WHERE termTaxId=%d AND
objectId IN (SELECT id FROM {
$wpdb->posts} WHERE postStatus='publish')",
$term->termId))); } include pluginDirPath(__FILE__).
'/views/metaboxes/my-custom-tax.php';
} ?>