// Sort facet values by WooCommerce term order

WooCommerce includes 2 built-in taxonomies: Product Categories and Product Tags. Both taxonomies support drag-and-drop ordering, compliments of WooCommerce:

WooCommerce drag-and-drop ordering

How it’s stored

WordPress core doesn’t have the ability to set custom term order. However, it does include a wp_termmeta table to allow extra data to be attached to terms.

WooCommerce simply saves the custom order into this table:

wp_termmeta table

WooCommerce also has hooks in place that, when calling the get_terms function, the resulting terms are automatically sorted.

Sort facet values by term order

Add the following code, and make sure to replace YOUR_FACET_NAME.

function fwp_term_orderby( $orderby, $facet ) {
    if ( 'YOUR_FACET_NAME' == $facet['name'] ) { // Replace "YOUR_FACET_NAME"
        $term_ids = get_terms( array(
            'taxonomy' => str_replace( 'tax/', '', $facet['source'] ),
            'fields' => 'ids',
        ) );
        if ( ! empty( $term_ids ) && ! is_wp_error( $term_ids ) ) {
            $term_ids = implode( ',', $term_ids );
            $orderby = "FIELD(f.term_id, $term_ids)";
        }
    }
    return $orderby;
}
add_filter( 'facetwp_facet_orderby', 'fwp_term_orderby', 10, 2 );