'How to add single product tag as body class?

I have seen a lot of references as to how to add a body tags based on WooCommerce categories, but what I am wanting to achieve is to add body classes from product tags.

Any pointers?



Solution 1:[1]

Something like this should work

function woo_body_classes($classes) {
    if(is_product_category()):
        global $wp_query;
        $current_products = wp_list_pluck( $wp_query->posts, 'ID' );
        $tags_list = array();
        foreach($current_products as $product):
            $tags = get_the_terms($product,'product_tag');
            if ( ! empty( $tags ) && ! is_wp_error( $tags ) ):
                foreach($tags as $tag):
                    $tags_list[] = $tag->name;
                endforeach;
            endif;
        endforeach;
        $tags_list = array_unique($tags_list);
        $classes[] = implode(' ',$tags_list);
    endif;
    return $classes;
}
add_filter( 'body_class','woo_body_classes' );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Mirchev