Skip to content
Advertisement

How to get attribute name instead of slug in variation?

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!

Advertisement

Answer

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;
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement