Get custom product attributes in Woocommerce

236,470

Solution 1

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'));

Solution 2

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.

Solution 3

September 2014:

$product->get_attribute( 'your_attr' );

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

Solution 4

You can get the single value for the attribute with below code:

$pa_koostis_value = get_post_meta($product->id, 'pa_koostis', true);

Solution 5

woocommerce_get_product_terms() is now (2014) deprecated.

Use wc_get_product_terms() instead.

Example:

global $product;
$koostis = array_shift( wc_get_product_terms( $product->id, 'pa_koostis', array( 'fields' => 'names' ) ) );
Share:
236,470
Nick
Author by

Nick

Updated on March 20, 2021

Comments

  • Nick
    Nick about 3 years

    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.

  • Ravi Soni
    Ravi Soni over 10 years
    Any idea for getting all attributes at once?
  • helgatheviking
    helgatheviking almost 10 years
    This is correct. I believe everything was soft-deprecated in version 2.1.
  • EHerman
    EHerman over 9 years
    @ravisoni get_post_meta( $prodict->id , '_product_attributes' );
  • Kristis
    Kristis about 6 years
    I'm get --> Notice: Only variables should be passed by reference. Woocommerce version 3.2.6. Code ---> $date = array_shift( wc_get_product_terms( $product->get_id(), 'pa_date', array( 'fields' => 'names' ) ) ); What can be wrong? I can't solve it. I get 'Null' of gettype($date)