Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the product attribute label name in Woocommerce 3

I am using a lot of product attributes on my products in Woocommerce and I am looping through all variations in a table that can be displayed with a shortcode on a product page.

For this table I need all the product attributes in the table head (this is before looping through the variations) and I get the attributes using:

$attributes = $product->get_variation_attributes();
foreach ($attributes as $key => $value) {
    echo '<td>'.&key.'</td>';
}

This isn't very elegant, is it?

So this works, too:

$attributes = $product->get_attributes();
foreach ($attributes as $attribute) {
    echo '<td>'$attribute['name']'</td>';
}

In both cases I get the slug of the product attribute. I need to get the label name instead, since there is a Polylang translation for each name (terms also).

How can I get the product attribute label name instead of the taxonomy slug?

like image 840
Renato Avatar asked Oct 31 '18 09:10

Renato


1 Answers

You will use wc_attribute_label() dedicated Woocommerce function:

foreach ($product->get_variation_attributes() as $taxonomy => $term_names ) {
    // Get the attribute label
    $attribute_label_name = wc_attribute_label($taxonomy);

    // Display attribute labe name
    echo '<td>'.$attribute_label_name.'</td>';
}

OR:

foreach ($product->get_attributes() as $taxonomy => $attribute_obj ) {
    // Get the attribute label
    $attribute_label_name = wc_attribute_label($taxonomy);

    // Display attribute labe name
    echo '<td>'.$attribute_label_name.'</td>';
}
like image 51
LoicTheAztec Avatar answered Oct 20 '22 11:10

LoicTheAztec