index woocommerce product attribute with hierarchy

<?php

add_filter( 'facetwp_index_row', function( $params, $class ) {

    if ( 'my_facet_name' ==  $params[ 'facet_name' ] ) { // replace 'my_facet_name' with name of facet to index with hierarchy

        if ( 0 === strpos( $params['facet_source'], 'cf/attribute_pa_' ) ) {
            $taxonomy = str_replace( 'cf/attribute_', '', $params['facet_source'] );
            $term = get_term_by( 'slug', $params['facet_value'], $taxonomy );

            if ( false !== $term ) {
                $params['term_id'] = $term->term_id;
                $params['parent_id'] = $term->parent;
                $params['facet_display_value'] = $term->name;
                $params['depth'] = count( get_ancestors( $term->term_id, $taxonomy, 'taxonomy' ) );
            }
        }

    } 

    return $params;

}, 10, 2 );