Woocommerce: How to loop through product variations?

I am building a Woocommerce site, and the client is going to sale headphones. Each headphone will have multiple colors. In the product’s index page he wants to display all product variations instead of 1 headphone.

For example, If i create 1 product and add the variations (color), in the index there will be 1 product, and I will be able to choose a variation in the product detail page.

Read More

But I could also create one category, let’s say ‘headphones’ and add each color as a single product and loop through that category to display the same product multiple times just with different colors.

What would be the best approach for this, and if the best approach is to create one single product with color variations, how do I loop through the variations in order to display each single color in the product’s index?

Related posts

2 comments

  1. <?php 
        $variations = $product->get_available_variations();
        foreach ( $variations as $key => $value ) {
    ?>
        <li>
            <span><?php echo $value['name']; ?></span>
        </li>
    <?php
        }
    ?>
    

    Maybe it could do what you’re askin’. Hope it helps

  2. I made that way:

    <?php 
      $versionvalues = get_the_terms( $product->id, 'pa_variacao');
      foreach ( $versionvalues as $versionvalue ) {
    ?>
        <li class="ui-state-default  ui-selectee" rel="<?php echo $versionvalue->term_id; ?>">
          <span class="ui-selectee"><?php echo $versionvalue->name; ?></span>
        </li>
    <?php
        }
    ?>
    

    Where ‘pa_variacao’ is ‘pa_’ the woocommerce prefix for the custom product variation and ‘variacao’ is the slug to the custom variation, the <li> is the way that I used to show the variations on my shop.

Comments are closed.