plugins – wp_query ‘s’ parameter does not work with WC_AJAX
Question
Hey developers I have a problem, I created a search input to filter products by wc_ajax but when it’s trying to filter the products it always shows that there is no product found
here is image:
and this is the php code:
function kia_test_ajax_search_callback() {
// Fetch All Products
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
);
// Start Filter Search Bar
if(!empty( $_POST['search-title'] )) {
$args['s'] = $_POST['search-title'];
}
var_dump($args);
$query = new WP_Query($args);
if ( $query->have_posts() ) :
do_action( 'woocommerce_before_shop_loop' );
woocommerce_product_loop_start();
while ( $query->have_posts() ) :
$query->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
woocommerce_product_loop_end();
do_action( 'woocommerce_after_shop_loop' );
else :
do_action( 'woocommerce_no_products_found' );
endif;
wp_reset_postdata();
}
add_action( 'wc_ajax_kia_search', 'kia_test_ajax_search_callback' );
this is jquery code:
<script>
(function($){
$(document).ready(function(){
$(document).on('submit', '#myform', function(e) {
e.preventDefault();
data = $(this).serialize();
console.log(data);
var settings = {
"url": "<?php echo WC_AJAX::get_endpoint( 'kia_search' ) ?>",
"method": "POST",
"data": data
}
$.ajax(settings).done(function (response) {
$('.products').html(response);
console.log(response);
});
});
});
})(jQuery);
</script>
i already have a product with hat name but the filter doen’t work why?
0
1 year
2022-02-13T09:43:11-05:00
2022-02-13T09:43:11-05:00 0 Answers
0 views
0
Leave an answer