1

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
    Oct 2, 2017 at 22:14

1 Answer 1

3
$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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.