Created
July 14, 2013 23:37
-
-
Save michaelaguiar/5996571 to your computer and use it in GitHub Desktop.
Woocommerce Products Per Page Selector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* WooCommerce */ | |
// Products per page | |
function woocommerce_catalog_page_ordering() | |
{ ?> | |
<form action="/shop" method="POST" name="results"> | |
<select name="woocommerce-sort-by-columns" id="woocommerce-sort-by-columns" class="sortby" onchange="this.form.submit()"> | |
<?php | |
$shopCatalog_orderby = apply_filters('woocommerce_sortby_page', array( | |
'' => __('Results per page', 'woocommerce'), | |
'2' => __('2 per page', 'woocommerce'), | |
'4' => __('4 per page', 'woocommerce'), | |
'6' => __('6 per page', 'woocommerce'), | |
'8' => __('8 per page', 'woocommerce'), | |
)); | |
foreach ($shopCatalog_orderby as $sort_id => $sort_name) { | |
$is_selected = (isset($_POST['woocommerce-sort-by-columns']) && (int) $_POST['woocommerce-sort-by-columns'] === (int) $sort_id) ? 'selected' : ((isset($_COOKIE['shop_pageResults']) && (int) $_COOKIE['shop_pageResults'] === (int) $sort_id) ? 'selected' : ''); | |
echo '<option value="' . $sort_id . '" ' . selected( $_SESSION['sortby'], $sort_id, false ) . ' '.$is_selected.'>' . $sort_name . '</option>'; | |
} | |
?> | |
</select> | |
</form> | |
<?php } | |
// Set Cookie | |
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, '/', 'keigan.dev', 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'); | |
add_action('woocommerce_before_shop_loop', 'woocommerce_catalog_page_ordering', 20); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment