17

I need to get attribute from woocommerce product variation.

$terms = get_post_meta($value['variation_id'], 'attribute_pa_color', true);

This code is giving me an attribute slug instead of name. How can I get attribute name?

Thank you so much in advance!

Pupik
  • 391
  • 1
  • 2
  • 13

4 Answers4

31

What you are getting is the slug of a taxonomy... In WooCommerce, attribute_pa_color without the attribute_ is a taxonomy.

So you can try something like this.. to get the term by slug. And get it's name.

$taxonomy = 'pa_color';
$meta = get_post_meta($value['variation_id'], 'attribute_'.$taxonomy, true);
$term = get_term_by('slug', $meta, $taxonomy);
echo $term->name;
Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
1

Native WooCommerce function wc_attribute_label() is the easiest way for this task.

Geza Gog
  • 180
  • 2
  • 7
0

You can easily find attributes by below code

 foreach ($product->attributes as $taxonomy => $attribute) {
   foreach ($attribute->get_terms() as $term) {
      var_dump($term); // term object data
   }
}

Sumon Sarker
  • 129
  • 2
  • 10
-1

You can try the following code.

$terms = get_the_terms( $value['variation_id'] , 'attribute_pa_color');

foreach ( $terms as $term ) {
    echo $term->name;
}

Let me know if that helped. Additional you can go through the explanation given in this link for more information and alternative solutions.

Domain
  • 11,562
  • 3
  • 23
  • 44
  • Not working, but I'll try to do sth about it. Thanks for hint! – Pupik Feb 01 '16 at 14:26
  • It works with this: get_the_terms( $product->id , 'pa_color'); but it returns attributes from all variations :( – Pupik Feb 01 '16 at 14:28
  • Tried to hard code variation ID instead of product ID and its not working. My guess is it is not supposed to be used this way (with variation ID)? – Pupik Feb 01 '16 at 14:31