Automattic\WooCommerce\Internal\ProductFilters
QueryClauses::get_price_filter_query_for_displayed_taxes
Get query for price filters when dealing with displayed taxes.
Method of the class: QueryClauses{}
No Hooks.
Returns
String. Constructed query.
Usage
// private - for code of main (parent) class only $result = $this->get_price_filter_query_for_displayed_taxes( $price_filter, $column, $operator ): string;
- $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. Accepts '>=' or '<='.
Default: '>='
QueryClauses::get_price_filter_query_for_displayed_taxes() QueryClauses::get price filter query for displayed taxes code WC 10.3.3
private function get_price_filter_query_for_displayed_taxes( float $price_filter, string $column = 'min_price', string $operator = '>=' ): string {
global $wpdb;
if ( ! in_array( $operator, array( '>=', '<=' ), true ) ) {
return '';
}
// Select only used tax classes to avoid unwanted calculations.
$cache_key = WC_Cache_Helper::get_cache_prefix( 'filter_clauses' ) . 'tax_classes';
$product_tax_classes = wp_cache_get( $cache_key );
if ( ! $product_tax_classes ) {
$product_tax_classes = $wpdb->get_col( "SELECT DISTINCT tax_class FROM {$wpdb->wc_product_meta_lookup};" );
wp_cache_set( $cache_key, $product_tax_classes );
}
if ( empty( $product_tax_classes ) ) {
return '';
}
$or_queries = array();
// 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
}