18

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.

clueless
  • 839
  • 1
  • 10
  • 24
  • Have you made any attempt to do this at all? – Mike Brant Dec 10 '13 at 16:49
  • Of course, I'm sorry. I forgot to post my code. – clueless Dec 10 '13 at 16:57
  • 1
    There is a much simpler way then to use code to do what you want. A woocommerce product can be a variable product and from there you can set the fields you want to have as a variable. Here is a link to some documentation for it: https://docs.woocommerce.com/document/variable-product/ – Ironer100 Jun 01 '17 at 14:43

5 Answers5

25

You can use get_terms() https://developer.wordpress.org/reference/functions/get_terms/

If you pass in pa_size or pa_color you will get back a list of terms in that taxonomy.

Adrian P.
  • 5,060
  • 2
  • 46
  • 47
Steven Jones
  • 674
  • 1
  • 6
  • 12
18

Hoping this is helpful to someone:

global $product; 

// Get product attributes
$attributes = $product->get_attributes();

if ( ! $attributes ) {
    echo "No attributes";
}

foreach ( $attributes as $attribute ) {

        echo $attribute['name'] . ": ";
        $product_attributes = array();
        $product_attributes = explode('|',$attribute['value']);

        $attributes_dropdown = '<select>';

        foreach ( $product_attributes as $pa ) {
            $attributes_dropdown .= '<option value="' . $pa . '">' . $pa . '</option>';
        }

        $attributes_dropdown .= '</select>';

        echo $attributes_dropdown;
}
runningonplants
  • 449
  • 3
  • 10
  • 3
    This surely can be helpful to someone. But who? And how? What does this code do? Make it mor eclear wit ha bit of explanation or comments in the code :) – Erenor Paz Dec 16 '16 at 10:51
  • 4
    OP is asking for a list of all attributes. Your solution is only on a single product level. – Bjorn Jan 30 '19 at 15:21
7

This post was written some time ago so I don't know if Woocommerce had this method in its previous incarnations.
For anyone else looking to do this, this line is all you need.

$product->list_attributes();

This allows you to customize the order and toggle whether or not you want to display the variation in the backend,

Christian Giupponi
  • 7,408
  • 11
  • 68
  • 113
user5029040
  • 79
  • 1
  • 1
5

In addition to @user5029040 answer, which outputs html, if you want to get an array you can use the following function.

$product->get_variation_attributes();
Arif
  • 79
  • 1
  • 7
  • 2
    Doesn't work outside of the loop: `Call to undefined method WC_Product_Simple::get_variation_attributes()` – Alexander Holsgrove Sep 17 '17 at 11:10
  • OP is asking for a list of all attributes. Your solution is only on a single product level. – Bjorn Jan 30 '19 at 15:21
  • @AlexanderHolsgrove This method is defined for `WC_Product_Variation`, not `WC_Product_Simple`. https://woocommerce.wp-a2z.org/oik_api/wc_product_variationget_variation_attributes/ – Melebius Nov 23 '21 at 08:58
  • If you don’t have the right `$product` in your scope, there is also `wc_get_product_variation_attributes()` which requires just the product ID. – Melebius Nov 23 '21 at 10:33
1

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.

Bas van Dijk
  • 816
  • 7
  • 20
  • 2
    Extra tip: when using `get_terms()` and specifying a `taxonomy` value, you have to prepend `pa_` to the `attribute_name` returned by `wc_get_attribute_taxonomis()`, OR use the built-in function `wc_attribute_taxonomy_name( "attribute_name" )` to get it automatically :) – Prid Jan 16 '23 at 01:16
  • 1
    Good one! Have forgot about that a few times haha – Bas van Dijk Jan 18 '23 at 16:02