Created
January 16, 2012 18:54
-
-
Save mikejolley/1622323 to your computer and use it in GitHub Desktop.
WooCommerce - Change default catalog sort order
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
/** | |
* 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 | |
} | |
If you want the catalog ordering to still work properly you should wrap your code in something like:
function woocommerce_catalog_orderby( $args ) {
if(!$_GET['orderby']) {
$args['orderby'] = 'menu_order';
$args['order'] = 'asc';
}
return $args;
}I was having a problem with a client wanting a specific order while also wanting the sorting to still function. This worked.
Thank you sir! 👍
@hoaiphatcr YOU saved my day!!!! thanks!!!
This Gist has been mentioned on Stack Overflow a few times:
https://stackoverflow.com/questions/47627079/sort-products-by-desc-order-in-woocommerce
A few other related threads:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi everyone, I'm deal with default sorting with meta key. Using ReHub theme + Woocomerce. I need to have sorted all product regarding their rehub_review_overall_score - it's meta key in ReHub theme.
//Adding custom sort
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_product_sorting' );
function custom_product_sorting( $args ) {
// Sort score DESC
if ( isset( $_GET['orderby'] ) && 'score-desc' === $_GET['orderby'] ) {
$args['meta_key'] = 'rehub_review_overall_score';
$args['orderby'] = 'meta_value_num';
$args['order'] = 'desc';
}
return $args;
}
//have custom sort as default
add_filter('woocommerce_default_catalog_orderby', 'default_catalog_orderby');
function default_catalog_orderby( $sort_by ) {
return 'score-desc';
}
The code is working properly only when I use it in URL, like ?orderby=score-desc but for default it's not sorting the products regarding their score.
Do you know where is the problem and how to solve it?
Thak you very much