Get custom product attributes in Woocommerce

In Woocommerce, I am trying to get product custom attribute values but I fail miserably and I don't get anything.

So I tried:

global $woocommerce, $post, $product;
$res = get_post_meta($product->id);
print_r(unserialize($res['_product_attributes'][0]));

And I'm getting this raw data:

[pa_koostis] => Array
        (
            [name] => pa_koostis
            [value] => 
            [position] => 0
            [is_visible] => 1
            [is_variation] => 0
            [is_taxonomy] => 1
        )

I know that there is a value because it is shown in the attribute section, but I just can't find a way to get it displayed with my custom code.

Solutions

Update for 2018. You can use:

global $product;
echo wc_display_product_attributes( $product );

To customise the output, copy plugins/woocommerce/templates/single-product/product-attributes.php to themes/theme-child/woocommerce/single-product/product-attributes.php and modify that.

Edited: The woocommerce_get_product_terms is deprecated since Woocommerce version 3

Go with the following as @datafeedr wrote in his answer:

global $product;
$koostis = array_shift( wc_get_product_terms( $product->id, 'pa_koostis', array( 'fields' => 'names' ) ) );

or even more compact:

global $product;
$koostis = $product->get_attribute( 'pa_koostis' );

Original answer:

$result = array_shift(woocommerce_get_product_terms($product->id, 'pa_koostis', 'names'));

September 2014:

$product->get_attribute( 'your_attr' );

You will need to define $product if it's not on the page.

Similar questions

Display specific custom product attributes on single product pages in Woocommerce
I found the following code to display all custom attributes on a product detail page (with a specific bar-style design that I need). The code works like a charm and I have the proper CSS to display horizontal bars of my custom attributes. Problem I have is that I only want to display specific named attributes and don't know how to change the loop t...
Display a custom text based on product attributes in Woocommerce variable product
I am trying display text on variable product page depending on current product's options. Say, if this product comes with (attributes) options#1 then display text#1, if this product is offered with (attributes) options#2 then display text#2 I have tried the following code but both text#1 and text#2 display for all variable products regardless of wh...
Show WooCommerce product attributes in custom home and product category archives
The below code that displays product attributes on product category archive pages and is based on "Display product attributes on specific Woocommerce product category archives page" answer thread. However, when I add that category on the homepage, It does not display the attributes. My code version: I can't figure out how to make it work on my cust...
get product attributes for current product and store it in a variable
Been trying to figure out how I can get the product attributes of the current product and store it in a variable and put it in a class. I've managed to get the product attributes, unfortunately it seems to be displaying all the products attributes to all the products I have set. Here is the code I have been working. After running the code here is w...
Get Product Attributes of a Product under a specific category Woocomerce
I have a product category archive page and i wanted to get all product attributes assigned to the products under that category. my attribute name is 'pa_brands' so i have this code but this returns all product attribute "brands" there are attributes that are specically assigned to a product under a category, any help is really appreciated
Wp add_shortcode attributes not changing the attributes value
I have created a shortcode with attributes (with the help of google), but It does not changing the attributes value. I have googled this issue a lot but didn't find solution. Here is my code: And I am calling it in my theme's index.php page like this: But it's not changing the value of attribute Background, I don't know where i am going wrong. It'...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.