How to make woocommerce product tags hierarchical?

I want to change the product tags in woocommerce to display as hierarchical.
I tried to look for a function or a hook to change this but can’t find any.

Will appreciate any help or suggestion to accomplish this.

Read More

Thank you.

Related posts

Leave a Reply

4 comments

  1. Tested on woocommerce 3.0.4

    function my_woocommerce_taxonomy_args_product_tag( $array ) {
        $array['hierarchical'] = true;
        return $array;
    };
    add_filter( 'woocommerce_taxonomy_args_product_tag', 'my_woocommerce_taxonomy_args_product_tag', 10, 1 );
    
  2. Found the solution, here it is if someone is looking to do the same:

    function reregister_taxonomy_pro_tags() {
        # the post types that the taxonomy is registered to
        $post_types = array('product');
        # set this to the taxonomy name
        $tax_name = 'product_tag';
        # load the already created taxonomy as array so we can
        # pass it back in as $args to register_taxonomy
        $tax = (array)get_taxonomy($tax_name);
    
        if ($tax) {
            # adjust the hierarchical necessities
            $tax['hierarchical'] = true;
            $tax['rewrite']['hierarchical'] = true;
    
            # adjust the hierarchical niceties (these could be ignored)
            $tax['labels']['parent_item'] = sprintf(__("Parent %s"),
            $tax->labels->singular_name);
            $tax['labels']['parent_item_colon'] = sprintf(__("Parent %s:"),
            $tax->labels->singular_name);
    
            # cast caps to array as expected by register_taxonomy
            $tax['capabilities'] = (array)$tax['cap'];
            # cast labels to array
            $tax['labels'] = (array)$tax['labels'];
            # register the taxonomy with our new settings
            register_taxonomy($tax_name, array('product'), $tax);
        }
    }
    

    init action with a late priority so other taxonomies are loaded

    add_action(‘init’, ‘reregister_taxonomy_pro_tags’, 9999);

  3. Thanks for the above answer saved me a ton of research but you should replace

        $tax['labels']['parent_item'] = sprintf(__("Parent %s"),
        $tax->labels->singular_name);
        $tax['labels']['parent_item_colon'] = sprintf(__("Parent %s:"),
        $tax->labels->singular_name);
    

    to

    $tax['labels']->parent_item = sprintf(__("Parent %s"),
    $tax['labels']->singular_name);
    $tax['labels']->parent_item_colon = sprintf(__("Parent %s:"),
    $tax['labels']->singular_name);
    
  4. Here’s how I did it for WooCommerce 3.4.5 (and WordPress 4.9.8). This is based on @user2293554’s answer.

    <?php
    function add_hierarchical_product_tags( $taxonomy, $object_type, $args ) {
        if ( $taxonomy == 'product_tag' && $args['hierarchical'] != true ) {
            $args['hierarchical'] = true;
            $args['rewrite']['hierarchical'] = true;
    
            // Update CMS labels
            $args['labels']->parent_item = sprintf(__("Parent %s"), $args['labels']->singular_name);
            $args['labels']->parent_item_colon = sprintf(__("Parent %s:"), $args['labels']->singular_name);
    
            // Convert to array for register_taxonomy()
            $args['labels'] = (array)$args['labels'];
            $args['capabilities'] = (array)$args['cap'];
            unset( $args['cap'] );  // remove the cap object
    
            register_taxonomy( $taxonomy, $object_type, $args );
        }
    }
    
    add_action( 'registered_taxonomy', 'add_hierarchical_product_tags', 10, 3 );
    

    The args['hierarchical'] != true condition is needed so we don’t get stuck in a loop, as the registered_taxonomy action will get called again.