Woocommerce ordring option rename and add options

<?php

// this actioon to remove default ordring
//remove_action(‘woocommerce_before_shop_loop’, ‘woocommerce_catalog_ordering’, 30);

function woocommerce_catalog_page_ordering() {
echo ‘<span class=”itemsorder”>’
?>
<form action=”” method=”POST” name=”results” class=”woocommerce-ordering”>
<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’),
’15’ => __(’15 Products per page’, ‘woocommerce’),
’30’ => __(’30 Products per page’, ‘woocommerce’),
’45’ => __(’45 Products per page’, ‘woocommerce’),
‘-1’ => __(‘All’, ‘woocommerce’),
));

foreach ($shopCatalog_orderby as $sort_id => $sort_name)
echo ‘<option value=”‘ . $sort_id . ‘” ‘ . selected($numberOfProductsPerPage, $sort_id, true) . ‘ >’ . $sort_name . ‘</option>’;
?>
</select>
</form>

<?php echo ‘ </span>’ ?>
<?php
}

add_action(‘woocommerce_before_shop_loop’, ‘woocommerce_catalog_page_ordering’, 20);

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, ‘/’, get_site_url(), 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;
}

// now we set our cookie if we need to
add_filter(‘loop_shop_per_page’, ‘dl_sort_by_page’);

function my_woocommerce_catalog_orderby($orderby) {
unset($orderby[“price-desc”]);
unset($orderby[“popularity”]);
unset($orderby[“rating”]);
unset($orderby[“date”]);
unset($orderby[“price”]);
return $orderby;
}

add_filter(“woocommerce_catalog_orderby”, “my_woocommerce_catalog_orderby”, 20);
/* function theme_sort_change($translated_text, $text, $domain) {
if (is_woocommerce()) {
switch ($translated_text) {
case ‘Sort by newness’ :
$translated_text = __(‘Date’, ‘woocommerce’);
break;
}
}
return $translated_text;
}
add_filter(‘gettext’, ‘theme_sort_change’, 20, 3); */

function custom_woocommerce_catalog_orderby($sortby) {
$sortby[‘title’] = __(‘Name’, ‘woocommerce’);
$sortby[‘price_asc’] = __(‘Price’, ‘woocommerce’);
$sortby[‘date_asc’] = __(‘Date’, ‘woocommerce’);
$sortby[‘popularity_asc’] = __(‘Popularity’, ‘woocommerce’);
/*$sortby[‘price_asc’] = ‘Price (Lowest to Highest)’;
$sortby[‘price_desc’] = ‘Price (Highest to Lowest)’;
$sortby[‘popularity_asc’] = ‘Popularity (Ranked Highest)’;
$sortby[‘popularity_desc’] = ‘Popularity (Ranked Lowest)’;
$sortby[‘sales_asc’] = ‘Sales (Highest to Lowest)’;
$sortby[‘sales_desc’] = ‘Sales (Lowest to Highest)’;
$sortby[‘date_desc’] = ‘Date (Newest to Oldest)’;
$sortby[‘date_asc’] = ‘Date (Oldest to Newest)’;
$sortby[‘title_asc’] = ‘Alphabetically (A-Z)’;
$sortby[‘title_desc’] = ‘Reverse-Alphabetically (Z-A)’;*/

return $sortby;
}

// Add option in orderby
add_filter(‘woocommerce_catalog_orderby’, ‘custom_woocommerce_catalog_orderby’);

function custom_woocommerce_get_catalog_ordering_args($args) {
$orderby_value = isset($_GET[‘orderby’]) ? woocommerce_clean($_GET[‘orderby’]) : apply_filters(‘woocommerce_default_catalog_orderby’, get_option(‘woocommerce_default_catalog_orderby’));

if ($orderby_value) {
switch ($orderby_value) :
case ‘price_asc’ :
$args[‘orderby’] = ‘meta_value_num’;
$args[‘order’] = ‘asc’;
$args[‘meta_key’] = ”;
break;

case ‘price_desc’ :
$args[‘orderby’] = ‘meta_value_num’;
$args[‘order’] = ‘desc’;
$args[‘meta_key’] = ‘_price’;
break;

case ‘popularity_asc’ :
$args[‘orderby’] = ‘rating’;
$args[‘order’] = ‘asc’;
$args[‘meta_key’] = ”;
break;

case ‘popularity_desc’ :
$args[‘orderby’] = ‘rating’;
$args[‘order’] = ‘desc’;
$args[‘meta_key’] = ”;
break;

case ‘sales_asc’ :
$args[‘orderby’] = ‘sales’;
$args[‘order’] = ‘asc’;
$args[‘meta_key’] = ”;
break;

case ‘sales_desc’ :
$args[‘orderby’] = ‘sales’;
$args[‘order’] = ‘desc’;
$args[‘meta_key’] = ”;
break;

case ‘date_asc’ :
$args[‘orderby’] = ‘date’;
$args[‘order’] = ‘asc’;
$args[‘meta_key’] = ”;
break;

case ‘date_desc’ :
$args[‘orderby’] = ‘date’;
$args[‘order’] = ‘desc’;
$args[‘meta_key’] = ”;
break;

case ‘title_asc’ :
$args[‘orderby’] = ‘title’;
$args[‘order’] = ‘asc’;
$args[‘meta_key’] = ”;
break;

case ‘title_desc’ :
$args[‘orderby’] = ‘title’;
$args[‘order’] = ‘desc’;
$args[‘meta_key’] = ”;
break;

endswitch;
}
return $args;
}

// Add option in orderby on change shooting pass the parameter
add_filter(‘woocommerce_get_catalog_ordering_args’, ‘custom_woocommerce_get_catalog_ordering_args’);
?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s