改善自定义开发的滚动条的性能

时间:2019-01-21 02:33:16

标签: php ajax wordpress woocommerce infinite-scroll

我正在尝试提高无限滚动代码的速度。这是我的ajax requet处理脚本

function ga_infinite_scroll() {//trigger this on infinite scroll
  add_filter( 'woocommerce_get_price_html', 'ga_show_price' );//filter to fix price range
  if(empty($_POST['search_term'] )){
    $params = json_decode( stripslashes( $_POST['query'] ), true );
    $params['post_status'] = 'publish';
    $params['posts_per_page'] = get_option('posts_per_page');
    $params['post_type'] = 'product';
    $params['paged'] = $_POST['page'] + 1; // we need next page to be loaded

  }
  else{//search  logic here
      $search_query = json_decode( stripslashes( $_POST['search_posts'] ), true );
      $search_query['post_status'] = 'publish';
      $search_query['posts_per_page'] = get_option('posts_per_page');
      $search_query['paged'] = $_POST['page'] + 1;
      wc_set_loop_prop( 'total', $_POST['search_count'] );
      $params = $search_query;

  }

  ob_start();           
  query_posts( $params);

  if ( have_posts() ) {//product loop
        if ( wc_get_loop_prop( 'total' ) ) {
              while ( have_posts() ) {
                the_post();
                wc_get_template_part( 'content', 'product' );
              }
            }
    } 

    $data = ob_get_clean();
    die($data); 
    exit;
}
add_action( 'wp_ajax_ga_infinite_scroll', 'ga_infinite_scroll' );
add_action( 'wp_ajax_nopriv_ga_infinite_scroll', 'ga_infinite_scroll' );

这是我的JavaScript:-

   jQuery(document).ready( function($) {
   var  url = window.location.origin + '/wp-admin/admin-ajax.php',
    canBeLoaded=true,
     bottomOffset = 2000; // the distance (in px) from the page bottom when you want to load more posts

    $(window).scroll(function(){
        var data = {
            'action': 'ga_infinite_scroll',
            'query': my_ajax_object.posts,
            'page' : my_ajax_object.current_page,
            //'search_results' : my_ajax_object.ga_search_results,
            'search_count' : my_ajax_object.ga_search_count,
            'search_posts': my_ajax_object.ga_search_posts,
            'search_term' : my_ajax_object.ga_search_term,
            'user_currency': my_ajax_object.user_currency,
            'reg_price_slug': my_ajax_object.reg_price_field_slug
        };


        if( $(document).scrollTop() > ( $(document).height() - bottomOffset ) && canBeLoaded == true ){

                $.ajax({//limit the ajax calls
                    url : url,
                    data:data,
                    type:'POST',                    
                    beforeSend: function( xhr ){
                        // you can also add your own preloader here
                        // you see, the AJAX call is in process, we shouldn't run it again until complete
                        //console.log(data.search_term);
                        $('#ajax-loader').show();  
                        canBeLoaded = false; 
                    },
                    success:function(data){
                        if( data ) {
                            $('#multiple-products .columns-3 .products ').find('li:last-of-type').after( data ); // where to insert posts

                            //console.log(url);
                            canBeLoaded = true; // the ajax is completed, now we can run it again
                            my_ajax_object.current_page++;
                            $('#ajax-loader').hide();
                        }
                        else{
                            $('#ajax-loader').html('End of products...').delay(1000).fadeOut(); 
                            return;
                        }

                    }
                });

        }
    });


    //setting if it's a search

});

我想知道使用query_posts是否是一个好主意,而我使用的这种过滤器在速度方面非常昂贵,但是如果我不使用它,最终会看到像这样的$ 15- $ 25的价格范围。关于如何处理这种情况以及查看ga_show_price的代码和我的入队脚本的任何想法,我都在这里Improving the speed of a custom infinite scroll上进行了添加,以及有关改进我的JavaScript的任何建议?谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用以下方法获得相关产品:

$array = wc_get_related_products( $product_id, $limit, $exclude_ids );