Get all variations attributes with prices for a variable product in WooCommerce
You can get all the data that you want for all product variations in a variable product this way:
if($product->is_type('variable')){ foreach($product->get_available_variations() as $variation ){ // Variation ID $variation_id = $variation['variation_id']; echo '<div class="product-variation variation-id-'.$variation_id.'"> <strong>Variation id</strong>: '.$variation_id.'<br>'; // Attributes $attributes = array(); foreach( $variation['attributes'] as $key => $value ){ $taxonomy = str_replace('attribute_', '', $key ); $taxonomy_label = get_taxonomy( $taxonomy )->labels->singular_name; $term_name = get_term_by( 'slug', $value, $taxonomy )->name; $attributes[] = $taxonomy_label.': '.$term_name; } echo '<span class="variation-attributes"> <strong>Attributes</strong>: '.implode( ' | ', $attributes ).'</span><br>'; // Prices $active_price = floatval($variation['display_price']); // Active price $regular_price = floatval($variation['display_regular_price']); // Regular Price if( $active_price != $regular_price ){ $sale_price = $active_price; // Sale Price } echo '<span class="variation-prices"> <strong>Price</strong>: '.$variation['price_html'].'</span><br> </div>'; }}
This code is tested and works.