WooCommerce Product Attributes are registered as a custom taxonomy. So we can simply use get_the_terms. Woo adds a prefix of ‘pa_’ to the name of your custom product attribute. So if you add an attribute called ‘Version’, who’s slug is ‘version’, the taxonomy would be ‘pa_version’. This is the attribute I use for the example below. You can pull up the value in a template file like so:
$versionvalues = get_the_terms( $product->id, 'pa_version'); foreach ( $versionvalues as $versionvalue ) { echo $versionvalue->name; }
Variation
In my case, I needed to show this within the customer’s ‘my account’ page, in the ‘Available downloads’ section. So I needed to get the product ID within the ‘downloads’ loop that loops through downloadable_products. I had to replace this:
$product->id
with this:
$download['product_id']
Jason
December 17th, 2012 at 4:27 am
Hi, i’ve been trying to get this code to work for an attribute named ‘colour’, and when i replace the word ‘version’ with the word ‘colour’, i continue to get the following error – “Warning: Invalid argument supplied for foreach() in /home/folder/public_html/mydomain.com/wp-content/themes/mytheme/woocommerce/cart/cart.php on line 84”
i’m trying to place this on the cart page, but it seems to be breaking in the “foreach()” part of your code. Any suggestions?
Isabel
December 23rd, 2012 at 5:18 pm
For the code to work, you must add the custom attribute when adding the product info. Name the attribute, ‘Colour’. Then your code in cart.php should be:
Matthew
December 21st, 2012 at 4:00 am
Thank you for sharing this info. I required the value of a specific hidden attribute created for all products in order to create some logic for sending product inquiry forms based on which store the product was located at to have the inquiry go to that stores specific email address.
This post got me there, so thank you ๐
Isabel
December 23rd, 2012 at 5:19 pm
Glad it was of use.
Chris Cedrone
January 10th, 2013 at 10:42 am
Thanks for this as it simplified my approach to what was normally a more convoluted process.
You rock!
federica
February 7th, 2013 at 12:13 pm
Hi isabel ๐
i have to print in my table in cart.php 2 coloumns, each one with a single attribute (size and colour).
by default both are printed under product’s title.
i wrote:
id, ‘pa_taglie’);
foreach ( $taglievalues as $taglievalue ) {
echo $taglievalue->name;
}
?>
but it returns tihis error:
Warning: Invalid argument supplied for foreach()
any suggestion?
sorry for my bad english ๐
Isabel
February 8th, 2013 at 8:18 pm
Part of your code is missing because I think you typed the php opening tag, so it must’ve been parsed and not printed here. So, I can’t see your first variable. I will try to look at it again if you paste your code without the php tags, please.
federica
February 9th, 2013 at 3:21 am
sorry! i don’t see it!
the code:
$terms = get_the_terms( $product->id, ‘taglie’);
foreach ( $terms as $term ) {
echo $term->name;
}
federica
February 9th, 2013 at 3:23 am
sorry… :S
$terms = get_the_terms( $product->id, ‘pa_taglie’);
foreach ( $terms as $term ) {
echo $term->name;
}
Isabel
February 10th, 2013 at 4:11 pm
That should work if you added a custom product attribute named “taglie” or “Taglie” when adding the new product in the WordPress admin area.
federica
February 11th, 2013 at 3:19 am
yes! i added a custom product attribute named “Taglie”
but when I add this code in my cart.php
it returns this error:
Warning: Invalid argument supplied for foreach()
what am I wrong?? :((
Mike
April 5th, 2013 at 10:50 am
Hi Federica
try this
if ( $coulourvalues = get_the_terms( $product->id, 'pa_marque')) {
foreach ( $coulourvalues as $coulourvalue ) {
echo $coulourvalue->name;
}
}
Copy this code where oyu appear this attribute
Sorry for my english
Isabel
April 5th, 2013 at 3:27 pm
Thank you, Mike. ( Merci )
I edited your comment to paste the code you sent me.
Mike
April 5th, 2013 at 10:53 am
if ( $coulourvalues = get_the_terms( $product->id, ‘pa_marque’)) {
foreach ( $coulourvalues as $coulourvalue ) {
echo $coulourvalue->name;
}
}
Ruben
April 10th, 2013 at 7:52 am
Hi, I have 5 product attributes but when I load the product page I want to display the attributes without having to click on the checkbox and click on add button. When I click on the attributes tab, I want to display all attributes. How can I do this?. I hope you can help me. Thank you very much, greetings.
Isabel
April 10th, 2013 at 10:29 am
I actually have not used WooCommerce since last year, and I know they made big changes in their 2.0 release which I haven’t looked at. Sorry; try looking in the WooCommerce docs or the WooCommerce forum.
Ruben
April 10th, 2013 at 10:39 am
Ok, I’ll look in the forum. Thanks for the quick reply
jordi
May 28th, 2013 at 11:22 am
Hi Isabel
I’m trying something a bit different and I don’t know how to solve it. I would like to show the description of the term and I write this:
id, 'pa_edicio');
foreach ( $versionvalues as $versionvalue ) {
echo $versionvalue->description;
}
?>
But the problem is that I have different variations of the product, so I would like to show only the description of the term selected.
IE: I’m selling t-shirts, and I have 3 colour terms. In each colour I have a description about this colour, so I want to show it whatever colour is selected.
Do you know how to do it?
Archimidis
July 12th, 2013 at 7:06 am
Great solution, you saved me!
I already used the snippet.
Nathan Crause
September 29th, 2013 at 3:28 pm
Fabulous post. I would never even have thought to look in the taxonomy for this information, and I’d struggled with this for a while now. All I can say is “thank you thank you thank you!”
Erwin
November 8th, 2013 at 5:47 am
Hi. It’s a very great arcticle.
I have a question.
How if I want to display the description only for selected attribute?
Mike
November 12th, 2013 at 3:55 am
This no longer seems to work, but you can get the same result this way instead:
$hello = $product->get_attribute( 'theattributename' );
echo $hello;
Note there is no need for the pa_ prexix.
Paolo
December 17th, 2013 at 4:30 am
Hi Isabel the following code preserve wordpress to don’t show any error if the attribute is not set:
$versionvalues = get_the_terms( $product->id, 'pa_version');
if(is_array($versionvalues)) {
foreach ( $versionvalues as $versionvalue ) {
echo $versionvalue->name;
}
}
Isabel
December 18th, 2013 at 4:55 pm
Yes, that’s important. Thank you.
whoaloic
February 19th, 2014 at 5:59 pm
What if I have more than one parameter for an attribute?
I need a way to write the comma as separator except for last item.
For now, I use this:
$colors = get_the_terms( $product->id, ‘pa_color’);
if(is_array($colorss)) {
foreach ( $colors as $color ) {
echo $color->name . ‘, ‘;
}
}
Regards
Chirag
February 27th, 2014 at 12:00 am
How to print the name of attribute before the attribute variables?
For example if attibute is version and it contains v1.0, v 2.0 ,v 2.5 etc.
Your snippet prints only v1.0
What if we want to print Version: v1.0
where “Version” is the attribute name?
Isabel
March 3rd, 2014 at 11:43 am
Please see: Show WooCommerce Custom Product Attributes Programmatically with functions
saurabh
July 14th, 2014 at 9:27 am
i use woocommerce in my site.and i want to display eg.book author name after the product name in product detail page.so i create a atribute for it. so hoe can i display this on product detal page?
/cogito/wp-content/plugins/woocommerce/templates/single-product/title.php here i want to display …..so please help me
Isabel
July 14th, 2014 at 9:13 pm
Please see this page instead: Show WooCommerce Custom Product Attributes Programmatically.
saurabh
July 15th, 2014 at 12:39 am
i saw this but i dont think this is helpful for me …plz tell me effective solution
Dwaynne
September 1st, 2015 at 12:08 pm
I tried the code to echo the product attribute anywhere on my page and it works fine. However, I get a ‘Warning: Invalid argument supplied for foreach()’ error for thos attributes that have no value in them. What do I need to add to check for no values in the attribute and echo a ‘No value’ for those? My research found the code below, but it echoes nothing at all, whether attributes have values or not :/
(the code intput textarea on this site’s comment form isn’t responding to text entry at all)
$dimensionsvalues = get_the_terms( $product->id, ‘pa_dimensions’);
if (is_array($allvalues))
{foreach ( $dimensionsvalues as $dimensionsvalue ) {
echo $dimensionsvalue->name;
}
}
Isabel
September 3rd, 2015 at 10:15 am
Hi,
You can check for a value by changing the 2nd line of your code to this:
Hope that helps.
Zaheer Abbas
May 9th, 2017 at 12:29 pm
Code is working for me if i change $product->id to $post->id
Nito Frerrer
October 15th, 2021 at 12:10 pm
How can i create a ajax search like this one:https://www.mrbooks.com/
I0m trying to implent it here on test.loneto.org