Live WooCommerce Product Search Form using AJAX (Shortcode)

Viewed 948

While the search form works, I need help fixing the problem that occurs when the form is empty after having been typed into.

What I mean is this: If you start typing a search query, you start to get results. This part works fine. But if you, while / after typing, remove the text from the search form - you get a list of ALL products.

It should only show results if and when typing and show nothing if and when empty.

Fix: After applying a modified version of the solution offered and provided by Terminator-Barbapapa as per below, it works as intended.

The code below might not be 100% correct, but it works.

The code so far:

    add_shortcode('live_search', 'live_search_function');
    function live_search_function() { ?>
    
        <input type="text" name="keyword" id="keyword" onkeyup="fetch()"></input>
    
        <div id="productfetch"></div>
    
        <?php
    }
    
add_action( 'wp_footer', 'ajax_fetch' );
function ajax_fetch() { ?>

    <script type="text/javascript">
    
        function fetch() {
            
            if( document.getElementById('keyword').value.trim().length == 0 ) {

                jQuery('#productfetch').html('');

            } else {

                jQuery.ajax( {

                    url: '<?php echo admin_url('admin-ajax.php'); ?>',
                    type: 'post',
                    data: { action: 'data_fetch', keyword: jQuery('#keyword').val() },
                    success: function(data) {
                        jQuery('#productfetch').html( data );
                    }
                });
            }
        }

    </script>
<?php
}
    
    add_action('wp_ajax_data_fetch' , 'product_fetch');
    add_action('wp_ajax_nopriv_data_fetch','product_fetch');
    function product_fetch() {
    
        $the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'product' ) );
    
        if( $the_query->have_posts() ) :
            while( $the_query->have_posts() ): $the_query->the_post(); ?>
        
        <h2><a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a></h2>
            <?php endwhile;
            wp_reset_postdata();
        endif;
    die();
    }
1 Answers

You can add a check in your fetch() function to see if your input field is empty. If so, clear the datafetch div, else run your AJAX.

add_action( 'wp_footer', 'ajax_fetch' );
function ajax_fetch() { ?>

    <script type="text/javascript">
    
        function fetch() {
            
            if( document.getElementById('keyword').value.trim().length == 0 ) {

                jQuery('#productfetch').html('');

            } else {

                jQuery.ajax( {

                    url: '<?php echo admin_url('admin-ajax.php'); ?>',
                    type: 'post',
                    data: { action: 'data_fetch', keyword: jQuery('#keyword').val() },
                    success: function(data) {
                        jQuery('#productfetch').html( data );
                    }
                });
            }
        }

    </script>
<?php
}
Related