Woocommerce Attributes pulling in wrong data

My Problem is very strange – I don’t fully understand all the aspects of woocommerce just yet and am still fiddling with things to understand it.

I’m trying to pull in the size attributes for each product – I’m succeeding in pulling in the attributes but I’m pulling in all of them for every product. Not the one’s I’m adding to the product via the custom field.

Read More

Example of the attributes that should be pulled in
link

But I’m pulling all 6 of my created attributes not the one’s selected for each product.
link

The code I’m using to pull in the attributes is the following

<?php 
        $terms = get_terms('pa_size');
        foreach ( $terms as $term ) {
        echo "<li>" .$term->name. "</li>";
         }
 ?>

Related posts

1 comment

  1. You are confusing get_terms() with get_the_terms()

    get_terms() returns an array all the terms in a taxonomy.

    get_the_terms() returns an array of all the terms for a particular post.

     global $post;
     $terms = get_the_terms( $post->ID, 'pa_size');
     foreach ( $terms as $term ) {
        echo "<li>" .$term->name. "</li>";
     }
    

    You might also like get_the_term_list() which would be like so:

    global $post;
    echo '<ul>';
    echo get_the_term_list( $post->ID, 'pa_size', '<li>', ',</li><li>', '</li>' );
    echo '</ul>';
    

Comments are closed.