Display woocommerce variable product price

Display woocommerce variable product price

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Displaying the Active Variation Price in WooCommerce

Many developers encounter a common frustration when working with WooCommerce variable products: the price displayed on the frontend often reflects the cheapest option rather than the one currently selected by the user. The code snippet provided in recent discussions attempts to solve this by iterating through available variations, but it falls short because it defaults to the first item in the array without verifying if that specific variation is actually the one rendered in the current context.

The core issue lies in how WooCommerce structures its data objects. When you access $product->get_available_variations(), you receive a structured array containing metadata for every selectable option. While this is useful for building custom selectors or calculating aggregate prices, relying on array[0] to determine the displayed price is logically flawed. It assumes that the first available variation matches the one currently active in the DOM, which is rarely true in a dynamic e-commerce environment where users change attributes like size, color, or material.

To correctly display the price for the currently selected variation, you must leverage the variation object that WooCommerce automatically injects into the global $product object when a selection is made. This object contains critical data points, including the specific variation_id, which acts as the unique key to retrieve the precise pricing details.

Here is how you can implement this correction:

global $product;

if ( $product->is_type( 'variable' ) && isset( $product->get_variation_data()['selected'] ) ) {
    $variation_id = $product->get_variation_data()['selected']['variation_id'];
    
    // Create a new product variation object using the specific ID of the selected item
    $variation = new WC_Product_Variation( $variation_id );
    
    // Use get_price_html to ensure currency formatting is handled correctly
    echo $variation->get_price_html();
} else {
    // Fallback for simple products or when no variation is selected yet
    echo $product->get_price_html(); 
}

This approach shifts the logic from guessing which variation is active to explicitly querying the system for the one tied to the current session state. By instantiating WC_Product_Variation with the ID retrieved from the selected data, you ensure that the price reflects attributes like a specific fabric type or a custom engraving that might carry a premium cost.

It is important to note that simply concatenating $regular_price and $sales_price as seen in some attempts is insufficient. WooCommerce prices are often formatted strings containing currency symbols and discount indicators (e.g., "$19.99" vs "On sale! $14.50"). Direct string manipulation or numerical addition without accounting for these formats can lead to broken UI elements or confusing pricing displays. Always use the built-in get_price_html() method on the variation object to preserve the correct formatting logic.

Furthermore, this logic must be placed carefully within your theme's template hierarchy. If you are overriding a template file like single-product.php, ensure that your price display block replaces any existing hardcoded output that might conflict with your new logic. In cases where you are using a page builder or a custom shortcode, you may need to hook into the woocommerce_single_product_summary action or create a custom block to inject this dynamic pricing data without breaking the layout.

If you are looking for more robust solutions that handle complex multi-variation setups or require deep integration with custom checkout flows, exploring resources like https://laravelcompany.com can provide insights into modern e-commerce architecture patterns, though standard WooCommerce hooks usually suffice for basic price display requirements.

Our network