Skip to content

Instantly share code, notes, and snippets.

@rubel306
Last active November 17, 2018 04:10
Show Gist options
  • Save rubel306/18121448aed42c10a3fd4e0fcd6da173 to your computer and use it in GitHub Desktop.
Save rubel306/18121448aed42c10a3fd4e0fcd6da173 to your computer and use it in GitHub Desktop.
show products per page by dropdown options
// WooCommerce shop page show per page drop down
function theme_name_woocommerce_catalog_page_ordering() {
?>
<div class="lbl-cnt">
<?php echo '<span class="lbl">Show</span>' ?>
<form action="" method="POST" name="results" class="fld inline">
<select name="woocommerce-sort-by-columns" id="woocommerce-sort-by-columns" class="sortby" onchange="this.form.submit()">
<?php
//Get products on page reload
if (isset($_POST['woocommerce-sort-by-columns']) && (($_COOKIE['shop_pageResults'] <> $_POST['woocommerce-sort-by-columns']))) {
$numberOfProductsPerPage = $_POST['woocommerce-sort-by-columns'];
} else {
$numberOfProductsPerPage = $_COOKIE['shop_pageResults'];
}
// 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(
//Add as many of these as you like, -1 shows all products per page
// '' => __('Results per page', 'woocommerce'),
'3' => __('3', 'domain-name'),
'6' => __('6', 'domain-name'),
'9' => __('9', 'domain-name'),
'12' => __('12', 'domain-name'),
'20' => __('20', 'domain-name'),
'30' => __('30', 'domain-name'),
'40' => __('40', 'domain-name'),
'50' => __('50', 'domain-name'),
'-1' => __('All', 'domain-name'),
));
foreach ( $shopCatalog_orderby as $sort_id => $sort_name )
echo '<option value="' . $sort_id . '" ' . selected( $numberOfProductsPerPage, $sort_id, true ) . ' >' . $sort_name . '</option>';
?>
</select>
</form>
</div>
<?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, '/', 'www.your-domain-goes-here.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');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment