woocommerce get attribute terms

phantomdentist picture phantomdentist · May 1, 2013 · Viewed 44.7k times · Source

In Woocommerce you can add global product attributes and terms. So for instance:

Size (attribute)
small  (term)
medium (term)
large  (term)

This is product independent. You can then select from the pre defined attributes on a product.

I need to get all of the terms in an attribute with php. So select the attribute required, eg size, and then return an array including [small,medium,large].

Seems simple enough but I can't find any help on doing this.

Answer

Mike Szostech picture Mike Szostech · Jun 7, 2013

Slightly confusing, especially when looking through the WooCommerce Docs since there is absolutely no mention of getting a list of the terms/attributes.

The Attributes are saved as a custom taxonomy, and the terms are taxonomy terms. That means you can use native Wordpress functions: Wordpress get_terms() Function Reference

By clicking on an attribute in WooCommerce, you can look in the URL and you can see they are all prepended with 'pa_'

This is likely what you need:

$terms = get_terms("pa_size");
foreach ( $terms as $term ) {
echo "<option>" . $term->name . "</option>";
}