sfsefsf33fs3fs3fs
sfsefsf33fs3fs3fs

Reputation: 653

Get WooCommerce Product Attribute label name

Variable/variations in woocommerce products.

I can do this to get the attribute value from pa_size: <?php echo $product_variation->get_attributes()['pa_size']; ?> which is somewhere in /wp-admin/edit.php?post_type=product&page=product_attributes.

But how do I get the pa_size label (in this case: 'Size')? Have tried to fetch everything based on post_type, page and then the "term_group". But that won't work. I can see that this usually is visible per default, but this is a custom solution. Also in https://github.com/woocommerce/woocommerce/blob/3.8.0/templates/single-product/product-attributes.php I can't see where they print the actual label, only the "Attribute child label and value". But not actual parent (pa_size => Size).

Have googled like a maniac for hours now.

Upvotes: 3

Views: 17271

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 254373

To get the label name of a WooCommerce product attribute you will use one of the 2 following ways:

1) Using Woocommerce wc_attribute_label() dedicated function:

$taxonomy   = 'pa_size';
$label_name = wc_attribute_label( $taxonomy );

2) Using wordPress get_taxonomy() function:

$taxonomy   = 'pa_size';
$label_name = get_taxonomy( $taxonomy )->labels->singular_name;

Upvotes: 5

H. Bloch
H. Bloch

Reputation: 488

In your case, you already know the taxonomy itself pa_size.

All product attributes are just taxonomies, with the pa_ prefix beforehand.

Then you will be able to print them using get_term_by()

it should look something like this:

<?php
$term = get_term_by('slug', $product_variation->get_attributes()['pa_size'], 'pa_size');
echo $term->name;
?>

Upvotes: 1

Related Questions