Single price from variation Type products
selected attributes Price
is_type( 'variable' ) ) {
?>
Product price with shortcode
is_type( 'variable' ) ) {
return ''; // Return nothing if the product is not a variable product
}
// Get all variations and determine the lowest price
$variations = $product->get_available_variations();
$lowest_price = null;
foreach ( $variations as $variation ) {
$price = floatval( $variation['display_price'] );
if ( is_null( $lowest_price ) || $price < $lowest_price ) {
$lowest_price = $price;
}
}
// Format the lowest price using WooCommerce formatting
$lowest_price_html = wc_price( $lowest_price );
// Initial output with only the lowest price displayed as the default
$output = '';
$output .= '' . $lowest_price_html . '';
$output .= '';
// Include JavaScript for dynamic price updates
add_action( 'wp_footer', function() use ( $lowest_price_html ) {
?>
