0

I am printing a stock/inventory report from WooCommerce. How can I echo the products variations i.e. product color or size, etc?

$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();

$product = new WC_Product_Variation( $loop->post->ID );
?>
<tr>
<td><?php echo $product->get_title(); ?></td>
<td><?php echo get_the_title( $loop->$product->get_attributes ); ?></td>
<td><?php echo $product->sku; ?></td>
<td><?php echo $product->stock; ?></td>
</tr>
<?php
endwhile;

I have tried the following but I get something like: Variation #49379

<?php echo get_the_title( $loop->get_post_meta( $thepostid, '_product_attributes', true ) ); ?>
Uncode
  • 179
  • 1
  • 5
  • 15
  • 2
    Possible duplicate of [woocommerce variations](http://stackoverflow.com/questions/13466898/woocommerce-variations) – Wolfgang Dec 29 '16 at 19:32

2 Answers2

0

You can use: echo $product->get_available_variations();

If this is a single style template, make sure you add global $product; near the top.

OnurK.
  • 121
  • 13
  • I get an error.... Fatal error: Uncaught Error: Call to undefined method WC_Product_Variation::get_available_variations() – Uncode Dec 29 '16 at 19:39
0

If you have product-id then this will be the solution

$product = wc_get_product($productid);
$variations = $product->get_available_variations();

Now you may loop through this variation to print all variations.

Hopefully, this will resolve your issue.

MakeWebBetter
  • 463
  • 3
  • 5