user5936336
user5936336

Reputation:

Get the values for a specific attribute from a product ID

I am trying to get products custom attribute with options values. This is how I get a normal attribute:

$myproducts['data']['discount'] = $product->get_attribute('pa_discount');

Does somebody have any idea how I get my attribute Size with options S, M and L?

Thanks

Upvotes: 4

Views: 7875

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

In your comment, you are using nearly the right way, but to get your attibute values you don't need array_shift() php function here, as you will get only the first listed value for that attribute.

You can use instead a foreach loop to get each value for example:

foreach( wc_get_product_terms( $product->id, 'pa_size' ) as $attribute_value ){
    // Outputting the attibute values one by one
    echo $attribute_value . '<br>';
}

Or you can use also implode() php function to get a coma separated string of that values:

echo implode(', ', wc_get_product_terms( $product_id, 'pa_size' )); 

This code works…

Upvotes: 2

Related Questions