Automattic\WooCommerce\StoreApi\Utilities

ProductQuery::get_price_filter_query_for_displayed_taxes()protectedWC 1.0

Get query for price filters when dealing with displayed taxes.

Method of the class: ProductQuery{}

No Hooks.

Return

String. Constructed query.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_price_filter_query_for_displayed_taxes( $price_filter, $column, $operator );
$price_filter(float) (required)
Price filter to apply.
$column(string)
Price being filtered (min or max).
Default: 'min_price'
$operator(string)
Comparison operator for column.
Default: '>='

ProductQuery::get_price_filter_query_for_displayed_taxes() code WC 9.3.3

protected function get_price_filter_query_for_displayed_taxes( $price_filter, $column = 'min_price', $operator = '>=' ) {
	global $wpdb;

	// Select only used tax classes to avoid unwanted calculations.
	$product_tax_classes = $wpdb->get_col( "SELECT DISTINCT tax_class FROM {$wpdb->wc_product_meta_lookup};" );

	if ( empty( $product_tax_classes ) ) {
		return '';
	}

	$or_queries = [];

	// We need to adjust the filter for each possible tax class and combine the queries into one.
	foreach ( $product_tax_classes as $tax_class ) {
		$adjusted_price_filter = $this->adjust_price_filter_for_tax_class( $price_filter, $tax_class );
		$or_queries[]          = $wpdb->prepare(
			'( wc_product_meta_lookup.tax_class = %s AND wc_product_meta_lookup.`' . esc_sql( $column ) . '` ' . esc_sql( $operator ) . ' %f )',
			$tax_class,
			$adjusted_price_filter
		);
	}

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared
	return $wpdb->prepare(
		' AND (
			wc_product_meta_lookup.tax_status = "taxable" AND ( 0=1 OR ' . implode( ' OR ', $or_queries ) . ')
			OR ( wc_product_meta_lookup.tax_status != "taxable" AND wc_product_meta_lookup.`' . esc_sql( $column ) . '` ' . esc_sql( $operator ) . ' %f )
		) ',
		$price_filter
	);
	// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared
}