Soporte » Plugins y Hacks » Problemas con búsqueda Ajax en el admin panel

  • Buenas noches estoy haciendo un plugin para hacer búsqueda de productos de woocommerce con búsqueda Ajax este es el código de la petición Ajax:

    
    add_action('wp_ajax_data_fetch' , 'data_fetch');
    add_action('wp_ajax_nopriv_data_fetch','data_fetch');
    function data_fetch(){
        $tax_query[] = array(
            'taxonomy'      => 'product_visibility',
            'field'         => 'name',
            'operator'      => 'NOT',
            'orderby'       => array('ASC','stock_status'),
        );
        $args_vdp = array('posts_per_page' => 7,
                          'tax_query' => $tax_query,
                          's' => esc_attr( $_POST['keyword'] ),
                          'post_type' => array('product_cat','product'),
                          'post_status' => 'publish',
                          
        );
        $the_query = new WP_Query($args_vdp);
        if( $the_query->have_posts() ) :
            while( $the_query->have_posts() ): $the_query->the_post(); global $product;?>
                <a href="<?php echo esc_url(post_permalink());?>" class="py-cart">
                        <h6 class="link-das-cat">
                            <?php the_title();?>
                            <span class="link-das-cat a-show-das">
                                <?php
                                    if($product->product_type=='variable'){
                                        $avail_vari = $product->get_available_variations();
                                        $comp_stock_variation_das=false;
                                        foreach ($avail_vari as $key => $value)
                                        {
                                            $vari_id = $value['variation_id'];
                                            $vari_colr = $value['attributes']['attribute_pa_colour'];
                                            $vari_obj = new WC_Product_variation($vari_id);
                                            $vari_stock = $vari_obj->get_stock_quantity();   
                                            $comp_stock_variation_das=true;
                                        }
                                        if($comp_stock_variation_das==true){
                                            echo wc_price($product->get_price());
                                        }
                                        
                                    }
                                    if($product->product_type=='simple'){
                                        if(!$product->is_in_stock())
                                        {
                                            echo wc_price($product->get_price());
                                        }else{
                                            echo 'Agotado';
                                        }
                                    }                                
                                ?>
                            </span>
                        </h6>
                </a><br>
            <?php endwhile;
        wp_reset_postdata();  
        // Mensaje de error al hacer la busqueda
        else: echo '
        <div class="container-error-icon-ajax-search">
            <div style="display:block;  width: 100%; text-align: center; padding-top:15px;">
                <h3>No se a encontrado</h3>
            </div>
            <div style="display:block; width: 100%; text-align: center; padding-bottom:15px;">
                <i class="fa fa-question fa-4x error-icon-ajax-search" aria-hidden="true"></i>
            </div>
        </div>';
        endif;
        die();
    }
    // Consulta ajax productos
    add_action( 'wp_footer', 'ajax_fetch' );
    function ajax_fetch() {?>
        <script type="text/javascript">
            function fetchResults(){
            var keyword = jQuery('#searchInput').val();
            if(keyword == ""){
                jQuery('#resultados_disortopedicos').html("");
            } else {
                jQuery.ajax({
                url: '<?php echo admin_url('admin-ajax.php'); ?>',
                type: 'post',
                data: { action: 'data_fetch', keyword: keyword  },
                beforeSend: function() {jQuery('#resultados_disortopedicos').html('<div class="con-load-icon-ajax-search"><i class="fa fa-spinner fa-spin load-icon-das-search fa-4x" aria-hidden="true"></i></div>');},
                success: function(data) { jQuery('#resultados_disortopedicos').html( data ); }
                });
            }   
            }
        </script>
        <?php
    }
    //Buscador y resultado
    <input class="field-disortopedicos-search" id="searchInput" name="s" type="text" onkeyup="fetchResults()" placeholder="'.esc_attr("Busqueda&hellip;").'" value="'.the_search_query().'">
    <div id="resultados_disortopedicos"></div>
    

    El problema que tengo es al usarlo en el panel admin me aparece el siguiente error

    
    Uncaught ReferenceError: fetchResults is not defined at HTMLInputElement.onkeyup (admin.php?page=pos-cont-dis:164:167)
    
    
    • Este debate fue modificado hace 1 año, 8 meses por daschin2.
Viendo 1 respuesta (de un total de 1)
Viendo 1 respuesta (de un total de 1)
  • El debate ‘Problemas con búsqueda Ajax en el admin panel’ está cerrado a nuevas respuestas.