Automattic\WooCommerce\Blocks\BlockTypes

ProductCollection::add_price_range_filter_posts_clauses()publicWC 1.0

Add the posts_clauses to the main query.

Method of the class: ProductCollection{}

No Hooks.

Return

null. Nothing (null).

Usage

$ProductCollection = new ProductCollection();
$ProductCollection->add_price_range_filter_posts_clauses( $clauses, $query );
$clauses(array) (required)
The query clauses.
$query(WP_Query) (required)
The WP_Query instance.

ProductCollection::add_price_range_filter_posts_clauses() code WC 9.4.2

public function add_price_range_filter_posts_clauses( $clauses, $query ) {
	$query_vars                  = $query->query_vars;
	$is_product_collection_block = $query_vars['isProductCollection'] ?? false;
	if ( ! $is_product_collection_block ) {
		return $clauses;
	}

	$price_range = $query_vars['priceRange'] ?? null;
	if ( empty( $price_range ) ) {
		return $clauses;
	}

	global $wpdb;
	$adjust_for_taxes = $this->should_adjust_price_range_for_taxes();
	$clauses['join']  = $this->append_product_sorting_table_join( $clauses['join'] );

	$min_price = $price_range['min'] ?? null;
	if ( $min_price ) {
		if ( $adjust_for_taxes ) {
			$clauses['where'] .= $this->get_price_filter_query_for_displayed_taxes( $min_price, 'min_price', '>=' );
		} else {
			$clauses['where'] .= $wpdb->prepare( ' AND wc_product_meta_lookup.min_price >= %f ', $min_price );
		}
	}

	$max_price = $price_range['max'] ?? null;
	if ( $max_price ) {
		if ( $adjust_for_taxes ) {
			$clauses['where'] .= $this->get_price_filter_query_for_displayed_taxes( $max_price, 'max_price', '<=' );
		} else {
			$clauses['where'] .= $wpdb->prepare( ' AND wc_product_meta_lookup.max_price <= %f ', $max_price );
		}
	}

	return $clauses;
}