WooCommerce Product per page Select option without reload

hi I’m trying without reload Woo-commerce Shop page product per page but its reload of selected number…
Now it’s working but page reload then work I need to without reload of shop page
anybody help me

add_action('wp_head','theme_ajaxurl');
function theme_ajaxurl() {
  ?>
  <script type="text/javascript">
  var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
  </script>
  <?php
}

add_action( 'wp_ajax_nopriv_dl_sort_by_page', 'woocommerce_catalog_per_page_ordering' );
add_action( 'wp_ajax_dl_sort_by_page', 'woocommerce_catalog_per_page_ordering' );


function woocommerce_catalog_per_page_ordering() {
?>
<form action="" method="POST" name="results">
<select name="woocommerce-sort-by-columns" id="woocommerce-sort-by-columns" class="sortby">
<?php

    //  This is where you can change the amounts per page that the user will use  feel free to change the numbers and text as you want, in my case we had 4 products per row so I chose to have multiples of four for the user to select.
      $shopCatalog_orderby = apply_filters('woocommerce_sortby_page', array(
        ''    => __('Products per page', 'woocommerce'),
        '8'   => __('8 per page', 'woocommerce'),
        '16'    => __('16 per page', 'woocommerce'),
        '32'    => __('32 per page', 'woocommerce'),
        '-1'    => __('View All', 'woocommerce'),
      ));

      foreach ( $shopCatalog_orderby as $sort_id => $sort_name )
        echo '<option value="' . $sort_id . '" ' . selected( $_SESSION['sortby'], $sort_id, false ) . ' >' . $sort_name . '</option>';
    ?>
</select>

</form>
<?php   // Adrian's code
  if (isset($_POST['woocommerce-sort-by-columns']) && (($_COOKIE['shop_pageResults'] <> $_POST['woocommerce-sort-by-columns']))) {
  $currentProductsPerPage = $_POST['woocommerce-sort-by-columns'];
  } else {
  $currentProductsPerPage = $_COOKIE['shop_pageResults'];
  }
  ?>
    <script type="text/javascript">
         jQuery('select.sortby>option[value="<?php echo $currentProductsPerPage; ?>"]').attr('selected', true); 
    </script>
<?php 
} 

// now we set our cookie if we need to
function dl_sort_by_page($count) {
  if (isset($_COOKIE['shop_pageResults'])) { // if normal page load with cookie

     $count = $_COOKIE['shop_pageResults'];


  }
  if (isset($_POST['woocommerce-sort-by-columns'])) { //if form submitted
    setcookie('shop_pageResults', $_POST['woocommerce-sort-by-columns'], time()+1209600, '/', 'yourdomain.com', false); //this will fail if any part of page has been output- hope this works!
    $count = $_POST['woocommerce-sort-by-columns'];
  }
  // else normal page load and no cookie
  return $count;
}


add_filter('loop_shop_per_page','dl_sort_by_page');

Script Code

<script>
  jQuery( document ).on( 'change', '#woocommerce-sort-by-columns', function(event) {
    event.preventDefault();
  var post_id = jQuery('select.sortby').val();
  jQuery.ajax({
    url : ajaxurl,
    type : 'POST',
    data : {
      action : 'dl_sort_by_page'    
    },
    success : function( response ) {
       alert('ok');
    }
  });

})

    </script>

Related posts