Automattic\WooCommerce\StoreApi\Schemas\V1

ProductSchema::get_price_range()protectedWC 1.0

Get price range from certain product types.

Метод класса: ProductSchema{}

Хуков нет.

Возвращает

Объект|null.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_price_range( $product, $tax_display_mode );
$product(\WC_Product) (обязательный)
Product instance.
$tax_display_mode(строка)
If returned prices are incl or excl of tax.
По умолчанию: ''

Код ProductSchema::get_price_range() WC 8.7.0

protected function get_price_range( \WC_Product $product, $tax_display_mode = '' ) {
	$tax_display_mode = $this->get_tax_display_mode( $tax_display_mode );

	if ( $product->is_type( 'variable' ) ) {
		$prices = $product->get_variation_prices( true );

		if ( ! empty( $prices['price'] ) && ( min( $prices['price'] ) !== max( $prices['price'] ) ) ) {
			return (object) [
				'min_amount' => $this->prepare_money_response( min( $prices['price'] ), wc_get_price_decimals() ),
				'max_amount' => $this->prepare_money_response( max( $prices['price'] ), wc_get_price_decimals() ),
			];
		}
	}

	if ( $product->is_type( 'grouped' ) ) {
		$children       = array_filter( array_map( 'wc_get_product', $product->get_children() ), 'wc_products_array_filter_visible_grouped' );
		$price_function = 'incl' === $tax_display_mode ? 'wc_get_price_including_tax' : 'wc_get_price_excluding_tax';

		foreach ( $children as $child ) {
			if ( '' !== $child->get_price() ) {
				$child_prices[] = $price_function( $child );
			}
		}

		if ( ! empty( $child_prices ) ) {
			return (object) [
				'min_amount' => $this->prepare_money_response( min( $child_prices ), wc_get_price_decimals() ),
				'max_amount' => $this->prepare_money_response( max( $child_prices ), wc_get_price_decimals() ),
			];
		}
	}

	return null;
}