<?php defined('ABSPATH') || exit;
class Extremes_Price
{
public static function additional_taxes($additional_taxes, $res = array())
{
if (!empty($additional_taxes)) {
$t = explode('+', $additional_taxes);
if (!empty($t) and is_array($t)) {
foreach ($t as $string) {
$tmp = explode(':', $string);
$tax_slug = $tmp[0];
$tax_terms = explode(',', $tmp[1]);
$slugs = array();
foreach ($tax_terms as $term_id) {
$term = get_term(intval($term_id), $tax_slug);
if (is_object($term)) {
$slugs[] = $term->slug;
}
}
if (!empty($slugs)) {
$res[] = array(
'taxonomy' => $tax_slug,
'field' => 'slug',
'terms' => $slugs
);
}
}
}
}
return $res;
}
public static function get_price($additional_taxes = "")
{
global $wpdb, $wp_the_query;
$args = $wp_the_query->query_vars;
$tax_query = isset($args['tax_query']) ? $args['tax_query'] : array();
if (is_object($wp_the_query->tax_query)) {
$tax_query = $wp_the_query->tax_query->queries;
}
$meta_query = isset($args['meta_query']) ? $args['meta_query'] : array();
$tax_query = self::additional_taxes($additional_taxes, $tax_query);
$temp_arr = array();
if (isset($args['taxonomy']) and isset($args[$args['taxonomy']]) and !empty($args[$args['taxonomy']])) {
$temp_arr = explode(',', $args[$args['taxonomy']]);
if (!$temp_arr or count($temp_arr) < 1) {
$temp_arr = array();
}
}
if (!empty($args['taxonomy']) && !empty($args['term'])) {
$tax_query[] = array(
'taxonomy' => $args['taxonomy'],
'terms' => (empty($temp_arr)) ? array($args['term']) : $temp_arr,
'field' => 'slug',
);
}
if (!empty($meta_query) and is_array($meta_query)) {
foreach ($meta_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 + 0.0) ) as min_price, max( CEILING( price_meta.meta_value + 0.0) )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 = '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'];
return $wpdb->get_row($sql);
}
public static function get_max_price($additional_taxes = "")
{
$prices = self::get_price($additional_taxes);
$max = ceil($prices->max_price);
return $max;
}
public static function get_min_price($additional_taxes = "")
{
$prices = self::get_price($additional_taxes);
$min = floor($prices->min_price);
return $min;
}
}