Automattic\WooCommerce\StoreApi\Schemas\V1
ProductSchema::get_price_range
Get price range from certain product types.
Method of the class: ProductSchema{}
No Hooks.
Returns
Object|null.
Usage
// protected - for code of main (parent) or child class $result = $this->get_price_range( $product, $tax_display_mode );
- $product(WC_Product) (required)
- Product instance.
- $tax_display_mode(string)
- If returned prices are incl or excl of tax.
Default:''
ProductSchema::get_price_range() ProductSchema::get price range code WC 10.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( ProductType::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( ProductType::GROUPED ) ) {
$children = $product->get_visible_children();
$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;
}