'product',
'posts_per_page' => $posts_per_page,
's' => $search_term,
'order' => $order,
'orderby' => $orderby,
'post_status' => array( 'publish' )
);
if( $orderby === 'price' ) {
$args['meta_key'] = '_price';
}
$output = '';
$qry = new WP_Query( $args );
if( $qry->have_posts() ) :
$output .= '
'. esc_html__( 'Products', 'botiga' ) .'
';
$output .= '
';
$output .= '';
while( $qry->have_posts() ) :
$qry->the_post();
$post = get_post();
$args = array(
'post_id' => $post->ID,
'type' => 'product'
);
ob_start();
botiga_get_template_part( 'template-parts/content', 'ajax-search-item', $args );
$output .= ob_get_clean();
endwhile;
$output .= '
';
endif;
// Categories
$show_categories = isset( $_POST['show_categories'] ) ? absint( $_POST['show_categories'] ) : 1;
if( $show_categories ) {
$args = array(
'taxonomy' => 'product_cat',
'name__like' => $search_term
);
$cats = get_terms( $args );
if( count( $cats ) > 0 && $search_term ) {
$output .= ''. esc_html__( 'Categories', 'botiga' ) .'
';
$output .= '
';
$output .= '';
foreach( $cats as $category ) {
$args = array(
'term_id' => $category->term_id,
'type' => 'category'
);
ob_start();
botiga_get_template_part( 'template-parts/content', 'ajax-search-item', $args );
$output .= ob_get_clean();
}
$output .= '
';
}
}
if( $output ) {
wp_send_json( array(
'status' => 'success',
'output' => wp_kses_post( $output )
) );
} else {
$output = ''. esc_html__( 'No products found.', 'botiga' ) .'
';
wp_send_json( array(
'status' => 'success',
'type' => 'no-results',
'output' => wp_kses_post( $output )
) );
}
}
add_action('wp_ajax_botiga_ajax_search_callback', 'botiga_ajax_search_callback');
add_action('wp_ajax_nopriv_botiga_ajax_search_callback', 'botiga_ajax_search_callback');