Created
September 4, 2018 03:33
-
-
Save trangsihung/c2af57b99d538bb80719a2937ff5db66 to your computer and use it in GitHub Desktop.
Get filtered min price for products
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Source: woocommerce\includes\widgets\class-wc-widget-price-filter.php:119 */ | |
function woo_get_filtered_price() { | |
global $wpdb; | |
$args = wc()->query->get_main_query()->query_vars; | |
$tax_query = isset( $args['tax_query'] ) ? $args['tax_query'] : array(); | |
$meta_query = isset( $args['meta_query'] ) ? $args['meta_query'] : array(); | |
if ( ! is_post_type_archive( 'product' ) && ! empty( $args['taxonomy'] ) && ! empty( $args['term'] ) ) { | |
$tax_query[] = array( | |
'taxonomy' => $args['taxonomy'], | |
'terms' => array( $args['term'] ), | |
'field' => 'slug', | |
); | |
} | |
foreach ( $meta_query + $tax_query as $key => $query ) { | |
if ( ! empty( $query['price_filter'] ) || ! empty( $query['rating_filter'] ) ) { | |
unset( $meta_query[ $key ] ); | |
} | |
} | |
$meta_query = new WP_Meta_Query( $meta_query ); | |
$tax_query = new WP_Tax_Query( $tax_query ); | |
$meta_query_sql = $meta_query->get_sql( 'post', $wpdb->posts, 'ID' ); | |
$tax_query_sql = $tax_query->get_sql( $wpdb->posts, 'ID' ); | |
$sql = "SELECT min( FLOOR( price_meta.meta_value ) ) as min_price, max( CEILING( price_meta.meta_value ) ) as max_price FROM {$wpdb->posts} "; | |
$sql .= " LEFT JOIN {$wpdb->postmeta} as price_meta ON {$wpdb->posts}.ID = price_meta.post_id " . $tax_query_sql['join'] . $meta_query_sql['join']; | |
$sql .= " WHERE {$wpdb->posts}.post_type IN ('" . implode( "','", array_map( 'esc_sql', apply_filters( 'woocommerce_price_filter_post_type', array( 'product' ) ) ) ) . "') | |
AND {$wpdb->posts}.post_status = 'publish' | |
AND price_meta.meta_key IN ('" . implode( "','", array_map( 'esc_sql', apply_filters( 'woocommerce_price_filter_meta_keys', array( '_price' ) ) ) ) . "') | |
AND price_meta.meta_value > '' "; | |
$sql .= $tax_query_sql['where'] . $meta_query_sql['where']; | |
$search = WC_Query::get_main_search_query_sql(); | |
if ( $search ) { | |
$sql .= ' AND ' . $search; | |
} | |
return $wpdb->get_row( $sql ); // WPCS: unprepared SQL ok. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment