1
votes

WHen I go to the product edit page, there is a tab 'Attributes'. And there I can set attribute names and their values.

I'm assuming this is how you add a custom attribute to a product on Woocommerce.

But how can I get this value in a loop?

I saw people using wc_get_product_terms but it wants me to pass the taxonomy and another array of arguments. What is the taxonomy!? I didn't add it manually. What are the arguments?

1
I just don't get what the heck the taxonomy has to do with it. Stuck on same problem.Avni Yayin

1 Answers

3
votes
$attributes = $product->get_attributes();

This will get you the attributes for a product or product variation.

foreach ( $attributes as $attribute ) {
    if ( $attribute['is_taxonomy'] ) {
        $values = wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'names' ) );
    } 
}

$product->id is product id.

$attribute['name'] will get you the product category/taxonomy. (You can print the array $attributes to find the field name)

array( 'fields' => 'names' ) is the argument which is optional to pass. Ignore it if not needed.