It's normal that it doesn't work as get_post_meta()
first function argument needs to be a numerical value but NOT a text string as 'id'
(the post ID)…
So you can't get any value for $attr_langs_meta
variable.
You can get the product ID in different ways:
1) From the WP_Post object with:
global $post;
$product_id = $post->ID;
2) From the WC_Product object with:
$product_id = $product->get_id();
Now the correct way for to do it is:
// The defined product attribute taxonomy
$taxonomy = 'pa_languages';
// Get an instance of the WC_Product Object (necessary if you don't have it)
// from a dynamic $product_id (or defined $product_id)
$product = wc_get_product($product_id);
// Iterating through the product attributes
foreach($product->get_attributes( ) as $attribute){
// Targeting the defined attribute
if( $attribute->get_name() == $taxonomy){
// Iterating through term IDs for this attribute (set for this product)
foreach($attribute->get_options() as $term_id){
// Get the term slug
$term_slug = get_term( $term_id, $taxonomy )->slug;
// Testing an output
echo 'Term Slug: '. $term_slug .'<br>';
}
}
}
Or also exclusively for a variable product (similar thing):
// The defined product attribute taxonomy
$taxonomy = 'pa_languages';
// Get an instance of the WC_Product Object (necessary if you don't have it)
// from a dynamic $product_id (or defined $product_id)
$product = wc_get_product($product_id);
// Iterating through the variable product variations attributes
foreach ( $product->get_variation_attributes() as $attribute => $terms ) {
// Targeting the defined attribute
if( $attribute == $taxonomy ){
// Iterating through term slugs for this attribute (set for this product)
foreach( $terms as $term_slug ){
// Testing an output
echo 'Term Slug: ' . $term_slug . '<br>';
}
}
}
This should work for you…