The problem with the order of goods when ajax pagination?

UPD. Found out that the problem is not in website, Ajax, guy in my php code. This is a bug with woocommerce. He does not know how to sort products with the same price. He gives them all the time in a different order, and if they fall on different pages, then there will be replays.

Hi all. Faced with such a problem. Wrote "infinite scrolling" for woocommerce, but there is an unpleasant problem that some posts may be repeated two times, maybe a little to disrupt the order, and I absolutely can not understand why. Once upon a time faced exactly the same problem on another site, but there it was not critical. There is one last item on one page coincide with the first item on the following.

here is my current code

//Here selection on katsanou taxonomy (if defined)
add_action( 'pre_get_posts', 'product_cat_modify_query' );

product_cat_modify_query function( $query ) {
 if ( ! is_admin() && $query->is_main_query() && is_product_category() ) {
 if (isset($_GET['brand'])) {
 $brandquery = array (
 'taxonomy' => 'product_brand',
 'field' => 'slug',
 'terms' => $_GET['brand']
);
 $tax_query = get_query_var('tax_query');
 $tax_query[] = $brandquery;
 $query->set('tax_query', $tax_query);
}
 } 
}

//Ajax action itself
add_action('wp_ajax_load_more_products', 'load_more_products');
add_action('wp_ajax_nopriv_load_more_products', 'load_more_products');

load_more_products function() {
 check_ajax_referer( 'qwkugf(Yoaywre9q=530fusefh9295@Tfgw3t*%*=', 'security' );

 $page = $_POST['page'];
 $terms = $_POST['terms'];
 $tax = $_POST['tax'];
 $brand = isset($_POST['brand']) ? $_POST['brand'] : null ;
 $woo_query = new WC_Query();
 $order_args = $woo_query->get_catalog_ordering_args();
 $return_html = ";

 $args = array(
 'post_type' => 'product',
 'posts_per_page' => bodymania_woocommerce_products_per_page(),
 'paged' => $page,
 'tax_query' => array(
 'relation' => 'AND',
array(
 'taxonomy' => $tax,
 'terms' => explode(',',$terms)
)
)
);
 $args = array_merge($args, $order_args);
 if ($brand) {
 $args['tax_query'][] = array(
 'taxonomy' => 'product_brand',
 'field' => 'slug',
 'terms' => $brand
);
}

 $return_products = new WP_Query($args);
 if ($return_products->have_posts()) {
 $max_pages = $return_products->max_num_pages;
ob_start();
 while ($return_products->have_posts()) {
$return_products->the_post();
 wc_get_template_part( 'content', 'product' );
}
wp_reset_postdata();
 $return_html .= ob_get_contents();
ob_end_clean();
 if ($page < $max_pages) {
 $next_page = $page + 1;
 $return_html .= '<a id="loadMoreProducts" href="#" data-page="'. $next_page .'" data-terms="'. $terms .'" data-tax="' . $tax . '">LOAD MORE</a>';
}
}
 echo $return_html;
wp_die();
}

It turns out that often
5a2e83e418a67326455039.png
-------------------------------------------------------------------------------------------------------------------
5a2e83ee6d154437084024.png

Moreover, after each page refresh, the order of the goods every time.
The total number of items is displayed in total scored is the same as in the database, but due to the fact that some products are repeated, in the end, does not appear all products.

UPD. So. found out that the problem with the goods with the same price. They are issued in arbitrary order. Ie there is 4 products with a price of 100, 1 is displayed on the current page 3 needs to get to the next. The next page will show three products with the price of 100, but each time in a different order, and one of them may be what was on the previous page.
June 10th 19 at 16:12
2 answers
June 10th 19 at 16:14
Solution
Threw ISU report them on github, promised to fixed and included in version 3.3
June 10th 19 at 16:16
If loaded in the script when deskroller until the end of the page, the problem is that each scroll wheel triggers your ajax request.
here or to opt out of the automatic loading and do just by clicking on load more button, or to track completion of the request, and then only, do the following. it is necessary to change the js, not php
Interesting. Tomorrow I will try.. I just scroll loaded. - aaron commented on June 10th 19 at 16:19
So. found out that the problem with the goods with the same price. They are issued in arbitrary order. Ie there is 4 products with a price of 100, 1 is displayed on the current page 3 needs to get to the next. The next page will show three products with the price of 100, but each time in a different order, and one of them may be what was on the previous page. JS is not, as it turned out, even though I fixed a scrolling - aaron commented on June 10th 19 at 16:22
when sorting by date, or sort "default", no such problem - aaron commented on June 10th 19 at 16:25

Find more questions by tags WooCommerceWordPress