103

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
ShintoTuna
  • 3,677
  • 8
  • 29
  • 36
  • The solution provided for this question is for 2.1 , you should update this code for working in woocommerce 3.0 , otherwise a huge log will be generate for deprecating – egmontt Jun 28 '17 at 13:36

9 Answers9

144

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'));
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
ShintoTuna
  • 3,677
  • 8
  • 29
  • 36
27

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.

Anders Norén
  • 181
  • 1
  • 2
  • 15
david_nash
  • 678
  • 7
  • 14
26

September 2014:

$product->get_attribute( 'your_attr' );

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

Lee Goddard
  • 10,680
  • 4
  • 46
  • 63
Artipixel
  • 1,246
  • 14
  • 17
19

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

$pa_koostis_value = get_post_meta($product->id, 'pa_koostis', true);
terrytsang
  • 207
  • 1
  • 2
19

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' ) ) );
Lee Goddard
  • 10,680
  • 4
  • 46
  • 63
datafeedr
  • 191
  • 1
  • 2
  • 5
    This is correct. I believe everything was soft-deprecated in version 2.1. – helgatheviking Jun 25 '14 at 14:37
  • 1
    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) – Kristis Feb 17 '18 at 18:31
16

Try this to get an array of attribute name => attribute value(s):

global $product;

$formatted_attributes = array();

$attributes = $product->get_attributes();

foreach($attributes as $attr=>$attr_deets){

    $attribute_label = wc_attribute_label($attr);

    if ( isset( $attributes[ $attr ] ) || isset( $attributes[ 'pa_' . $attr ] ) ) {

        $attribute = isset( $attributes[ $attr ] ) ? $attributes[ $attr ] : $attributes[ 'pa_' . $attr ];

        if ( $attribute['is_taxonomy'] ) {

            $formatted_attributes[$attribute_label] = implode( ', ', wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'names' ) ) );

        } else {

            $formatted_attributes[$attribute_label] = $attribute['value'];
        }

    }
}

//print_r($formatted_attributes);

return $formatted_attributes;

It's little inefficient but does the trick.

airdrumz
  • 429
  • 4
  • 9
5

Although @airdrumz solutions works, you will get lots of errors about you doing it wrong by accessing ID directly, this is not good for future compatibility.

But it lead me to inspect the object and create this OOP approach:

function myplug_get_prod_attrs() {
    // Enqueue scripts happens very early, global $product has not been created yet, neither has the post/loop
    global $product;
    $wc_attr_objs = $product->get_attributes();
    $prod_attrs = [];
    foreach ($wc_attr_objs as $wc_attr => $wc_term_objs) {
        $prod_attrs[$wc_attr] = [];
        $wc_terms = $wc_term_objs->get_terms();
        foreach ($wc_terms as $wc_term) {
            array_push($prod_attrs[$wc_attr], $wc_term->slug);
        }
    }
    return $prod_attrs;
}

Bonus, if you are performing the above early before the global $product item is created (e.g. during enqueue scripts), you can make it yourself with:

    $product = wc_get_product(get_queried_object_id());
run_the_race
  • 1,344
  • 2
  • 36
  • 62
3

The answer to "Any idea for getting all attributes at once?" question is just to call function with only product id:

$array=get_post_meta($product->id);

key is optional, see http://codex.wordpress.org/Function_Reference/get_post_meta

jere_hr
  • 284
  • 3
  • 11
2

Use below code to get all attributes with details

    global $wpdb;

    $attribute_taxonomies = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_name != '' ORDER BY attribute_name ASC;" );
    set_transient( 'wc_attribute_taxonomies', $attribute_taxonomies );

    $attribute_taxonomies = array_filter( $attribute_taxonomies  ) ;

    prin_r($attribute_taxonomies);
Nishad Up
  • 3,457
  • 1
  • 28
  • 32