title: Loadmore products - how to set the value of current category products count?
body: <p>I tried many ways to use loadmore products, including plugins, and <a href="https://coderoad.ru/52189258/%D0%98%D1%81%D0%BF%D0%BE%D0%BB%D1%8C%D0%B7%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5-AJAX-%D0%B4%D0%BB%D1%8F-%D0%B7%D0%B0%D0%B3%D1%80%D1%83%D0%B7%D0%BA%D0%B8-%D0%B1%D0%BE%D0%BB%D1%8C%D1%88%D0%B5%D0%B3%D0%BE-%D0%BA%D0%BE%D0%BB%D0%B8%D1%87%D0%B5%D1%81%D1%82%D0%B2%D0%B0-%D0%BF%D1%80%D0%BE%D0%B4%D1%83%D0%BA%D1%82%D0%BE%D0%B2-WooCommerce" rel="nofollow noreferrer">here</a> I've found the way, that I like.
  Php part</p>
  <pre><code>/**
   * Load next 12 products using AJAX
   */
  function ajax_next_posts() {
  global $product;
  // Build Query
      $args = array(
              'post_type'             =&gt;  'product',
              'posts_per_page'        =&gt;  (int)$_POST['posts_per_page'],
              'orderby'               =&gt;  'title',
              'order'                 =&gt;  'ASC',
              'offset'                =&gt;  (int)$_POST['post_offset'],
          );
  
  if( !empty( $_POST['product_cat'] ) ) {
      $args['tax_query'] = array(
          'relation'  =&gt; 'AND',
              array (
                  'taxonomy'  =&gt;  'product_cat',
                  'field' =&gt;  'id',
                  'terms' =&gt; $_POST['product_cat'],
                  'operator'  =&gt;  'IN'
              ),
              array (
                  'taxonomy'  =&gt;  'pa_color',
                  'field' =&gt;  'id',
                  'terms' =&gt; $_POST['pa_color'],
                  'operator'  =&gt;  'IN'
              ),
              array (
                  'taxonomy'  =&gt;  'pa_size',
                  'field' =&gt;  'id',
                  'terms' =&gt; $_POST['pa_size'],
                  'operator'  =&gt;  'IN'
              ),
      );
  }
  
  $count_results = '0';
  
  $ajax_query = new WP_Query( $args );
  
  // Results found
  if( $ajax_query-&gt;have_posts() ){
  
      // Start &quot;saving&quot; results' HTML
      $results_html = '';
      ob_start();
  
      while( $ajax_query-&gt;have_posts() ) {
  
          $ajax_query-&gt;the_post();
          echo wc_get_template_part( 'content', 'product' );
  
      }
      wp_reset_postdata();
  
      // &quot;Save&quot; results' HTML as variable
      $results_html = ob_get_clean();
  
  } else {
  
      // Start &quot;saving&quot; results' HTML
      $results_html = '';
      ob_start();
  
      echo &quot;That's all!&quot;;
  
  
      // &quot;Save&quot; results' HTML as variable
      $results_html = ob_get_clean();
  
  }
  
  // Build ajax response
  $response = array();
  
  // 1. value is HTML of new posts and 2. is total count of posts
  array_push ( $response, $results_html );
  echo json_encode( $response );
  
  // Always use die() in the end of ajax functions
  die();
  }
  add_action('wp_ajax_ajax_next_posts', 'ajax_next_posts');
  add_action('wp_ajax_nopriv_ajax_next_posts', 'ajax_next_posts');
  
  </code></pre>
  <p>JS part</p>
  <pre><code>/**
   * AJAX Load (Lazy Load) events
   */
  
   //--   Settings
  
  // How many do you want to load each button click?
  var postsPerPage = 1;
  
  //--    /Settings
  
  // How many posts there's total
  var totalPosts = parseInt( jQuery( '#found-posts' ).text() );
  
  // if( totalPosts == postOffset ) {
  //  jQuery( '#load-more' ).fadeOut();
  // }
  
  jQuery('#load-more').click( function(e){
      e.preventDefault();
  
      // Get current category
      var cat_id  =   jQuery(this).data('product-cat');
  
  
  ajax_next_posts( cat_id );
      jQuery('body').addClass('ajaxLoading');
  });
  
  var ajaxLock = false; // ajaxLock is just a flag to prevent double clicks and spamming
  
  if( !ajaxLock ) {
  
      function ajax_next_posts( cat_id ) {
  
          ajaxLock = true;
  
          // How many have been loaded
          var postOffset = jQuery( '.product' ).length;
  
  
          // Ajax call itself
          jQuery.ajax({
              method: 'POST',
              url: woocommerce_params.ajax_url,
              data: {
                  action: 'ajax_next_posts',
                  post_offset: postOffset,
                  posts_per_page: postsPerPage,
                  product_cat: cat_id
              },
              dataType: 'json'
          })
          .done( function( response ) { // Ajax call is successful
  
              // Add new posts
              jQuery( '.products' ).append( response[0] );
  
              // Update Post Offset
              var postOffset = jQuery('.product').length;
  
              ajaxLock = false;
  
              console.log( 'Success' );
  
              jQuery('.product').addClass('col-lg-4 col-6');
  
              jQuery('body').removeClass('ajaxLoading');
  
              // How many posts there's total
              console.log( &quot;Total Posts: &quot; + totalPosts );
  
              // How many have been loaded
              var postOffset = jQuery( '.product' ).length
              console.log( &quot;Posts on Page: &quot; + postOffset );
  
              // Hide button if all posts are loaded
              if( ( totalPosts - postOffset ) &lt;= 0 ) {
                  jQuery( '#load-more' ).fadeOut();               
              }
  
          })
          // .fail( function() {
          .fail( function(jqXHR, textStatus, errorThrown) { // Ajax call is not successful, still remove lock in order to try again
  
              ajaxLock = false;
  
              console.log(XMLHttpRequest);
              console.log(textStatus);
              console.log(errorThrown);
  
              console.log( 'Failed' );
  
          });
      }
  }
  
  }); 
  </code></pre>
  <p>It works, if to set the value of var totalPosts = parseInt( jQuery( '#found-posts' ).text() ); manually. Now that's a static div. But how to make it dynamic? The value of current category or taxonomy.</p>
  <p>I've found how to find the cound of all products, for all catalog for example</p>
  <pre><code>function product_count_shortcode( ) {
          $count_posts = wp_count_posts( 'product' );
   return $count_posts-&gt;publish;
  }
  add_shortcode( 'product_count', 'product_count_shortcode' );
  </code></pre>
  <p>And then I place it to that static div</p>
  <pre><code>&lt;div class=&quot;d-none&quot;  id=&quot;found-posts&quot;&gt;
      &lt;?php echo do_shortcode('[product_count]'); ?&gt;
  &lt;/div&gt;
  </code></pre>
  <p>I need the same for product categories.</p>