How to set Load more button for custom taxonomy products using Ajax

Question

I want to show more products for Load more button but my tax_query is not working for that particular taxonomy category. So how can I get the current taxonomy name to find the products?

My Taxonomy Filter:

$custom_taxterms = wp_get_object_terms( $post->ID, 'product-category', array('fields' => 'ids') );

I am using $custom_taxterms variable in posting data.

My Jquery:

jQuery(document).ready( function($) {
    var productpage_all = 1;

    jQuery("#loadproducts").on("click",function(){
       
        $("#loadproducts").attr("disabled",true);
        var count= jQuery("#loadproducts").attr("rel");
        var total_page  =   Math.ceil(count/ppp);
        console.log(jQuery.fn.jquery);
        jQuery.ajax({
            type : 'POST',
            url  : ajaxUrl,
            data : {
                action          : 'more_product_taxonomy',   //function name
                offset          : (productpage_all * ppp) ,
                ppp             : ppp,
                page            : productpage_all
                tax_post        : <?php echo $custom_taxterms; ?>
            },
            success: function(posts){
                productpage_all++;
                $(".show-products").append(posts);
                
                var res = productpage_all * ppp;
                if(res > count){
                    var resfound = res-count;
                    var resfound1 = res-resfound;
                }else{
                    resfound1 = productpage_all * ppp;
                }
                $("#loadproducts").attr("disabled",false);

                if(productpage_all  ==  total_page){
                    $(".product-items-more").empty();
                }
            }
        });
   });
});

My functions.php

add_action('wp_ajax_nopriv_more_product_taxonomy', 'more_product_taxonomy');
add_action('wp_ajax_more_product_taxonomy', 'more_product_taxonomy');
function more_product_taxonomy(){
$offset = $_POST["offset"];
$ppp = $_POST["ppp"];
$page = $_POST["page"];
$custom_taxterms = $_POST['tax_post'];
$args = array(
    'post_type' => 'product',
    'posts_per_page' => $ppp,
    'paged' => $page,
    'offset' =>     $offset,
    'post_status' => 'publish',
    'tax_query' => array(
            array(
                'taxonomy' => 'product-category',
                'terms' => $custom_taxterms,
            )
        ),
);
query_posts( $args );
while(have_posts()) : the_post();
    get_template_part( 'template-parts/more', 'product' );
endwhile; wp_reset_query();
exit;
}
0
Manvendra Singh 4 months 0 Answers 14 views 0

Leave an answer