Guest User

Untitled

a guest
May 25th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. // WooCommerce number of results select dropdown
  2. function woocommerce_catalog_page_ordering() {
  3. ?>
  4. <?php echo '<span class="itemsorder">Results per page:' ?>
  5. <form action="" method="POST" name="results" class="woocommerce-ordering">
  6. <select name="woocommerce-sort-by-columns" id="woocommerce-sort-by-columns" class="sortby" onchange="this.form.submit()">
  7. <?php
  8.  
  9. //Get products on page reload
  10. if (isset($_POST['woocommerce-sort-by-columns']) && (($_COOKIE['shop_pageResults'] <> $_POST['woocommerce-sort-by-columns']))) {
  11. $numberOfProductsPerPage = $_POST['woocommerce-sort-by-columns'];
  12. } else {
  13. $numberOfProductsPerPage = $_COOKIE['shop_pageResults'];
  14. }
  15.  
  16. // 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.
  17. $shopCatalog_orderby = apply_filters('woocommerce_sortby_page', array(
  18. //Add as many of these as you like, -1 shows all products per page
  19. // '' => __('Results per page', 'woocommerce'),
  20. '10' => __('10', 'woocommerce'),
  21. '20' => __('20', 'woocommerce'),
  22. '30' => __('30', 'woocommerce'),
  23. '40' => __('40', 'woocommerce'),
  24. '50' => __('50', 'woocommerce'),
  25. '-1' => __('All', 'woocommerce'),
  26. ));
  27.  
  28. foreach ( $shopCatalog_orderby as $sort_id => $sort_name )
  29. echo '<option value="' . $sort_id . '" ' . selected( $numberOfProductsPerPage, $sort_id, true ) . ' >' . $sort_name . '</option>';
  30. ?>
  31. </select>
  32. </form>
  33.  
  34. <?php echo ' </span>' ?>
  35. <?php
  36. }
  37.  
  38. // now we set our cookie if we need to
  39. function dl_sort_by_page($count) {
  40. if (isset($_COOKIE['shop_pageResults'])) { // if normal page load with cookie
  41. $count = $_COOKIE['shop_pageResults'];
  42. }
  43. if (isset($_POST['woocommerce-sort-by-columns'])) { //if form submitted
  44. setcookie('shop_pageResults', $_POST['woocommerce-sort-by-columns'], time()+1209600, '/', 'www.your-domain-goes-here.com', false); //this will fail if any part of page has been output- hope this works!
  45. $count = $_POST['woocommerce-sort-by-columns'];
  46. }
  47. // else normal page load and no cookie
  48. return $count;
  49. }
  50.  
  51. add_filter('loop_shop_per_page','dl_sort_by_page');
  52. add_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_page_ordering', 20 );
Add Comment
Please, Sign In to add comment