WooCommerce AJAX Pagination no plugin

169
August 14, 2018, at 7:10 PM

I am trying to look into how I could create my own WooCommerce AJAX Pagination function. All the tutorials I've found use a plugin and I would prefer not to. I have created an AJAX Pagination function before for another website but, it's not as simple with WooCommerce as the product posts on the shop page are generated automatically, where as before I had created my own loop for getting the content which I then converted into a JSON object to use in JS.

So my questions are, is it possible to plug this functionality into WooCommerce using the original products loop? Or am I going to have to create a new loop for getting the products, but will this cause issues with loading times as the original WooCommerce loop would still run too?

Here's the php for how I achieved AJAX Pagination before on another website, could I somehow plug the WooCommerce loop into this function?

<?php
    add_action('wp_ajax_nopriv_load_more', 'load_more');
    add_action('wp_ajax_load_more', 'load_more');
    function load_more(){
        $paged = $_POST["page"]+1;
        $args = array(
            'post_type'         => 'customer_testimonial',
            'posts_per_page'    => 2,
            'paged'             => $paged,
            'orderby'           => 'date'
        );
        $query = new WP_Query( $args );
        $no_of_posts = $query->found_posts;
        if( $query->have_posts() ):
            while( $query->have_posts() ): $query->the_post(); 
                $post_cont[] = array(
                    "name"  =>  get_field('customer_name'),
                    "area"  =>  get_field('area'),
                    "review"    => get_field('review')
                );
            endwhile; 
        endif;
        // echo json_encode($result);
        $max_posts[] = array(
            "no_of_posts"   => $no_of_posts
        );
        echo json_encode(array( "post_cont" => $post_cont, "max_posts" => $max_posts));
        wp_reset_postdata();
        die();
    }
?>
READ ALSO
Prevent array_merge from merging an array if one of the specified argument is not an array

Prevent array_merge from merging an array if one of the specified argument is not an array

I want array_merge to merge only when the argument specified is an array else skip that argument

161
get hierarchy of users by parent

get hierarchy of users by parent

I am trying to get all child and grand child and their child in PHP

112
Get the top selling product&#39;s current inventory in mysql

Get the top selling product's current inventory in mysql

I want to get the top selling product's current inventory from the databaseI have successfully fetch the top-selling products but unable to fetch their current inventory in the same query

100
phpmydatagrid doesnt work on PHP 7? [on hold]

phpmydatagrid doesnt work on PHP 7? [on hold]

I am using this mysql editor on my admin dashboard and it has been working good so far, but after I made a new DB (mysql 57) it stopped working

139