WC_Product_Variable::get_price_html( string $price = '' )

Returns the price in html format.


Description Description

Note: Variable prices do not show suffixes like other product types. This is due to some things like tax classes being set at variation level which could differ from the parent price. The only way to show accurate prices would be to load the variation and get it’s price, which adds extra overhead and still has edge cases where the values would be inaccurate.

Additionally, ranges of prices no longer show ‘striked out’ sale prices due to the strings being very long and unclear/confusing. A single range is shown instead.


Parameters Parameters

$price

(Optional) Price (default: '').

Default value: ''


Top ↑

Return Return

(string)


Top ↑

Source Source

File: includes/class-wc-product-variable.php

	public function get_price_html( $price = '' ) {
		$prices = $this->get_variation_prices( true );

		if ( empty( $prices['price'] ) ) {
			$price = apply_filters( 'woocommerce_variable_empty_price_html', '', $this );
		} else {
			$min_price     = current( $prices['price'] );
			$max_price     = end( $prices['price'] );
			$min_reg_price = current( $prices['regular_price'] );
			$max_reg_price = end( $prices['regular_price'] );

			if ( $min_price !== $max_price ) {
				$price = wc_format_price_range( $min_price, $max_price );
			} elseif ( $this->is_on_sale() && $min_reg_price === $max_reg_price ) {
				$price = wc_format_sale_price( wc_price( $max_reg_price ), wc_price( $min_price ) );
			} else {
				$price = wc_price( $min_price );
			}

			$price = apply_filters( 'woocommerce_variable_price_html', $price . $this->get_price_suffix(), $this );
		}

		return apply_filters( 'woocommerce_get_price_html', $price, $this );
	}


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.