Reputation: 6449
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?
Upvotes: 1
Views: 10700
Reputation: 332
$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->get_id(), $attribute['name'], array( 'fields' => 'names' ) );
}
}
$product->get_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.
Upvotes: 3