Show Only Default Price on Variable Products

3d Animation

For variable products in WooCommerce, prices are displayed in a range, e.g., from £10 – £15. With this JavaScript override, we can show only the default price. This is sometimes useful to show the most common (default) price on products.


/**
 * Show the default variation price instead of the price range.
 */
add_filter( 'woocommerce_variable_price_html', 'show_default_variation_price', 10, 2 );
add_filter( 'woocommerce_variable_sale_price_html', 'show_default_variation_price', 10, 2 );

function show_default_variation_price( $price, $product ) {

    $default_attributes = $product->get_default_attributes();

    // No default variation set
    if ( empty( $default_attributes ) ) {
        return $price;
    }

    foreach ( $product->get_available_variations() as $variation_data ) {

        $variation = wc_get_product( $variation_data['variation_id'] );

        $matches = true;

        foreach ( $default_attributes as $attribute => $value ) {
            if ( $variation->get_attribute( $attribute ) !== $value ) {
                $matches = false;
                break;
            }
        }

        if ( $matches ) {
            return $variation->get_price_html();
        }
    }

    return $price;
}