How to echo a attribute label in woocommerce?

Im trying to echo the products attribute labels inside a class in a woocommerce loop.
I have tried:

class="<?php echo wc_attribute_label( $name ); ?>"

but it doesn’t work. What am I doing wrong?

Read More

Thanks for any help possible!

Kind regards,
Richard

Related posts

2 comments

  1. Thanks to Kharis Sulistiyono i got this answer in the wordpress/woocommerce forums:

    <?php
    global $product;
    $attribute = $product->get_attributes();
    
    $attribute_arr = array();
    
    if( count($attribute) > 0 ){
        foreach ($attribute as $key => $value) {
            $attribute_arr[] = $key;
        }
    }
    
    $attributes = implode($attribute_arr, ' ');
    ?>
    
    /* Print as class value */
    class="<?php echo esc_attr($attributes); ?>";
    

    It works like a charm!

  2. You can do this:

    $term = get_term_by( 'name', $variation_product->get_attribute( 'pa_color' ), 'pa_color' );
    

    This gives you a term object, so if you for instance want the labels slug you can just:

    echo $term->slug;
    

    Or ID:

    echo $term->id; 
    

Comments are closed.