Reputation: 7587
In Woocommerce 3.3.4, I have a variable product with two attributes (pa_size and pa_color). I have just one combination of those two attributes with the following data:
"attributes": [
{
"id": 1,
"name": "Size",
"option": "1"
},
{
"id": 2,
"name": "color",
"option": "Blue"
}
]
However, when I call the wc_get_product_terms( 13174, "pa_size" );
where the id is the variation id of the product, it returns an empty array. In all other cases, it does return the term. Any idea how to debug it?
Edit:
If I use another variation ID from a product with more than one variation, I do get a result like this ["XL"]
from the above code.
Upvotes: 0
Views: 3076
Reputation: 254231
Product attributes in product variation are stored in database as post meta data, but not as a post term.
To get a product attribute value you will need to use WC_Product
method get_attribute()
instead. You will get a term name (string):
$taxonomy = 'pa_color'; // The product attribute taxonomy
$product = wc_get_product( $product_id ); // Get the WC_Product object
$label_name = get_taxonomy( $taxonomy )->labels->singular_name; // The taxonomy label name
$term_name = $product->get_attribute( $taxonomy ); // The term name
$term = get_term_by( 'name', $term_name, $taxonomy ); // The WP_Term object
The "product_variation" post type dont handle any taxonomy or custom taxonomy by default
Upvotes: 4