public
Last active

WooCommerce - Change default catalog sort order

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8
/**
* This code should be added to functions.php of your theme
**/
add_filter('woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby');
 
function custom_default_catalog_orderby() {
return 'date'; // Can also use title and price
}

Thank you!! I was getting frustrated trying to do this.

strangely, the site has reverted to using ABC order, even with this code in place. maybe something changed when I moved to the most current version (from 1.3 to 1.4). it was working. this code made both the front page and any tags show the way I wanted. Both now have reverted with 0 code changes made within the child theme I am using based on the twenty-eleven theme.

Like @mcsolas mentioned, the given code did not work.

But this worked:

add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $args ) {
    $args['meta_key'] = '_price';
    $args['orderby'] = 'meta_value_num';
    $args['order'] = 'desc'; 
    return $args;
}

Note that this code sorts by price - highest to lowest. Doing it on date is simpler. Skip the 'meta_key', and set 'orderby' as 'date'.

HTH!

when I upgraded from 1.3 to 1.4 I had already applied the function in my theme and it was already showing most recent first on my site. on upgrade, the site "cached" a the old default ABC value, somehow didn't seem to care about my theme override and started showing the site in ABC order again. Eventually support in their forum suggested that it was a browser issue and to clear the cookies / history. It worked, the site started showing the most recent product first again.

is there a way to sort it via sku? or maybe sort by custom meta box?

I liked the way this code defaulted the sorting to price highest to lowest, but it also makes it so that the sort alphabetically or by most recent not work... I just want the default to be sort by highest to lowest price, but still have the other two options available that work.. any ideas? Thanks!!

Is it possible for this function to 'show all' products?

Hope you can help..

I've been going nuts trying to find the actual filter woocommerce_catalog_orderby -- where is it?

I too want to be able to sort by whether something is in stock or not - ie adding additional sort functionality -- but not sure how best to go about it.

grimmwerks, the woocommerce_catalog_orderby filter is located in woocommerce/templates/loop/sorting.php .

mikejolley's initial code worked fine for me. Incidentally, his function can also return another value which I have used so that the 'order' attribute of my products are used to sort the products...

function custom_default_catalog_orderby() {
return 'menu_order'; // Can also use title, date and price
}

@jhodgski the option 'default' returns 'menu_order' for the orderby argument anyway, no need to update it with this function.

@ jperocho use this code to Sort by SKU

add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $args ) {
    $args['meta_key'] = '_sku';
    $args['orderby'] = 'meta_value';
    $args['order'] = 'asc'; 
    return $args;
}

How would you go about changing the default sorting for a single category?

Hi
and may rotate randomly when updating?

I used the Sort by SKU code and it works. Only problem is numeric sku's are sorting like text instead of numbers. 1,10,100,101 etc. How change I make it so the order is normal numeric. 1,2,3,4,5 etc?

niravmehta and jatinsapra's code both work, thank you. However the other sorting criteria that comes with woocommerce plugin in templates/loop/orderby.php like title, price etc will no longer work when I add this meta sorting to functions file.

I need to be able to sort by the various options provided with the plugin as well as several custom fields that I have added to my products. How can I integrate these meta sorting options with what is already in place?

What if I just wanted featured products to display first?

A client has some quirky feature request. Having one particular product going always first and the rest products are in alphabetical order..
Any ideas on that?

^
using @jatinsapra, update these

$args['meta_key'] = '_featured';
$args['order'] = 'desc';

Cheers!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.