Skip to content

Instantly share code, notes, and snippets.

@flayder
Created September 20, 2020 19:00
Show Gist options
  • Save flayder/9d21e62fb6be979193a66efdb8480b94 to your computer and use it in GitHub Desktop.
Save flayder/9d21e62fb6be979193a66efdb8480b94 to your computer and use it in GitHub Desktop.
add_filter('woocommerce_shortcode_products_query', 'product_query_function', 10, 3);
function product_query_function($query_args, $attributes, $type) {
$get = $_GET;
if(!empty($get)) {
foreach ($get as $key => $value) {
if(strpos($key, "filter_") !== false && $value && strpos($key, "product_visivility") === false) {
//if(!isset($query_args["meta_query"]["relation"])) $query_args["meta_query"]["relation"] = "AND";
$attribute = str_replace("filter_", "", $key);
$query_args["tax_query"][] = [
'taxonomy' => "pa_".$attribute,
'field' => 'slug',
'terms' => (is_array($value)) ? $value : [$value]
];
}
}
if(isset($get["min_price"]) && $get["min_price"] > 0 && isset($get["max_price"]) && $get["max_price"] > 0) {
$query_args["meta_query"][] = [
'key' => '_price',
'value' => array($get["min_price"], $get["max_price"]),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
];
} elseif(isset($get["min_price"]) && $get["min_price"] > 0 && !isset($get["max_price"])
|| isset($get["min_price"]) && $get["min_price"] > 0 && $get["max_price"] <= 0) {
$query_args["meta_query"][] = [
'key' => '_price',
'value' => $get["min_price"],
'compare' => '>='
];
} elseif(isset($get["max_price"]) && $get["max_price"] > 0 && !isset($get["min_price"])
|| isset($get["max_price"]) && $get["max_price"] > 0 && $get["min_price"] <= 0) {
$query_args["meta_query"][] = [
'key' => '_price',
'value' => $get["max_price"],
'compare' => '<='
];
}
}
return $query_args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment