Create a custom taxonomy for WooCommerce without a plugin

92 views Asked by At

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?

1

There are 1 answers

0
LoicTheAztec On

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):

add_action('init', 'register_woocommerce_custom_taxonomy');
function register_woocommerce_custom_taxonomy() {
    $text_domain = 'woocommerce';

     // Register this custom taxonomy for "product" post type
    register_taxonomy('collection', array('product'), array(
        'labels'            => array(
            'name'              => _x('Collections', 'taxonomy general name', $text_domain),
            'singular_name'     => _x('Collection', 'taxonomy singular name', $text_domain),
            'search_items'      => __('Search Collection', $text_domain),
            'all_items'         => __('All Collection', $text_domain),
            'parent_item'       => __('Parent Collection', $text_domain),
            'parent_item_colon' => __('Parent Collection:', $text_domain),
            'edit_item'         => __('Edit Collection', $text_domain),
            'update_item'       => __('Update Collection', $text_domain),
            'add_new_item'      => __('Add New Collection', $text_domain),
            'new_item_name'     => __('New Collection Name', $text_domain),
            'menu_name'         => __('Collections', $text_domain),
        ),
        'hierarchical'      => true, // Like categories
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'show_in_rest'      => true,
        'rewrite'           => array('slug' => 'collection'),
    ) ); 
}

Code goes on functions.php file of your child theme (or in a plugin). Tested and works.

enter image description here