woocommerce get list of attribute values

Viewed 82718

I'm using woocommerce on wordpress to create a simple shop site and I've added a couple attributes to a product. These are namely, size and color. Under size I have a variety of values including Small, Medium and Large. Same with color ie. Red, Blue, Green.

What I want to do is show these values in a dropdown. Basically just list them out so I can use the values as filters for the shop catalog page.

Any help would be great.

EDIT: I've delved into the woocommerce code and api docs and only found this code to pull the attributes.

global $woocommerce;

$attr_tax = $woocommerce->get_attribute_taxonomy_names();

foreach( $attr_tax as $tax ) {
     echo $woocommerce->attribute_taxonomy_name( $tax->attribute_name );
}

What this snippet gives me are the taxonomy slugs only, ie. pa_size and pa_color. I'm very new to woocommerce, but a search in there api docs reveals nothing about how to pull the values of these attributes.

5 Answers

If you wanted to retrieve all of the values for every wc taxonomy.

You could use the function: wc_get_attribute_taxonomies(). This will return an array like:

[
  "id:24" => {
    "attribute_id": "24"
    "attribute_name": "bending-lines"
    "attribute_label": "Bending lines"
    "attribute_type": "select"
    "attribute_orderby": "menu_order"
    "attribute_public": "0"
  }
]

Then you could use the function get_terms() to retrieve the values per taxonomy.

Related