Software Developer

Show WooCommerce Custom Product Attributes in a Template

This page is in the Code Graveyard where I lay to rest snippets of code that I don't use anymore. Some have lost their usefulness due to the evolving nature of the web, while others have been replaced with better code. "Be Ye Warned!"

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']

By

We've 33 Responses

  1. 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?

    avatar
    • 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:

      $coulourvalues = get_the_terms( $product->id, 'pa_coulour');
       
            foreach ( $coulourvalues as $coulourvalue ) {
             echo $coulourvalue->name;
              }
      
      avatar
  2. 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 🙂

    avatar
  3. 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 😛

    avatar
    • 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.

      avatar
  4. 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?? :((

    avatar
  5. 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

    avatar
  6. April 5th, 2013 at 10:53 am

    if ( $coulourvalues = get_the_terms( $product->id, ‘pa_marque’)) {
    foreach ( $coulourvalues as $coulourvalue ) {
    echo $coulourvalue->name;
    }
    }

    avatar
  7. 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.

    avatar
  8. 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?

    avatar
  9. 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.

    avatar
  10. 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;
    }
    }

    avatar
  11. 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

    avatar
  12. 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?

    avatar
  13. 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

    avatar
  14. 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;
    }
    }

    avatar
  15. May 9th, 2017 at 12:29 pm

    Code is working for me if i change $product->id to $post->id

    
    <?php
    
    $dimensionsvalues = get_the_terms( $post->id, 'pa_color');
    print_r($dimensionsvalues);
    if (is_array($allvalues))
    {foreach ( $dimensionsvalues as $dimensionsvalue ) {
    echo $dimensionsvalue->name;
    }
    }
    
     ?>
    
    
    
    avatar

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]