There is a need to display Attribute Variations with their respect "Regular Price". However despite trying there is no success to display price. Please see the code below that displays the Variation Fine. Please help out to display the Price as well.
//Getting product attributes
$product_attributes = $product->get_attributes();
if(!empty($product_attributes)){
//Getting product attributes slugs
$product_attribute_slugs = array_keys($product_attributes);
$count_slug = 0;
echo '<div class="product_attributes">';
foreach ($product_attribute_slugs as $product_attribute_slug){
$count_slug++;
// Removing "pa_" from attribute slug and adding a cap to first letter
$attribute_name = ucfirst( str_replace('pa_', '', $product_attribute_slug) );
//echo $attribute_name . ' (';
$attribute_values = get_terms($product_attribute_slug);
$count_value = 0;
//print_r(array_values($available_variations));
foreach($attribute_values as $attribute_value){
$count_value++;
$attribute_name_value = $attribute_value->name; // name value
$attribute_slug_value = $attribute_value->slug; // slug value
$attribute_slug_value = $attribute_value->term_id; // ID value
echo $attribute_name_value;
}
}
echo '</div>';
//print_r(array_values($attribute_values));
}
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.