Woocommerce Search Product Query
Copy Below Code
View As A Text File
Show Text Only
Show API
Edit Code
$searchArr=[];
$tax_queryArr=[];
if(isset($getArr['rating']) && $getArr['rating']!=''){
$searchArr[] = array(
'key' => '_wc_average_rating',
'type' =>'numeric',
'value' => $getArr['rating'].'.00' ,
'compare' => '>='
);
}
if (isset($getArr['size']) && $getArr['size']!='') {
$tax_queryArr[] =
array(
'taxonomy' => 'pa_size',
'field' => 'slug',
'terms' => array($getArr['size']),
'operator' => 'IN',
);
}
if (isset($getArr['color']) && $getArr['color']!='') {
// $searchArr[] =array(
// 'key' => '_product_attributes',
// 'value' => $getArr['color'],
// 'compare' => 'LIKE'
// );
}
if (isset($getArr['watt']) && $getArr['watt']!='') {
// $searchArr[] =array(
// 'key' => '_product_attributes',
// 'value' => $getArr['watt'],
// 'compare' => 'LIKE'
// );
}
$minPrice= $getArr['min_price'] ?? '';
$maxPrice= $getArr['max_price'] ?? '';
if ($minPrice!='' && $maxPrice=='') {
$searchArr[] =array(
'key' => '_price',
'type' =>'numeric',
'value' => $minPrice,
'compare' => '>='
);
}
else if ($minPrice=='' && $maxPrice!='') {
$searchArr[] =array(
'key' => '_price',
'type' =>'numeric',
'value' => $maxPrice,
'compare' => '<='
);
}
else if ($minPrice!='' && $maxPrice!='') {
$searchArr[] =array(
'key' => '_price',
'type' =>'numeric',
'value' => [$minPrice,$maxPrice],
'compare' => 'BETWEEN'
);
}
// echo '<pre>';print_r( $tax_queryArr);exit;
// echo '<pre>';print_r( $searchArr);exit;
$term = get_queried_object();
$args = array(
'product_cat' => $term->slug ?? '',
'post_type' => 'product',
'orderby' => 'title',
'meta_query' => array(
$searchArr
)
);
if(sizeof($tax_queryArr)>0){
$args['tax_query'] =array(
$tax_queryArr
);
}
$query = new WP_Query( $args );
// echo $GLOBALS['wp_query']->request;
// exit;
if( $query->have_posts()){ while( $query->have_posts() ){
$query->the_post();
//CODE HERE
}}
?>