Skip to content

Instantly share code, notes, and snippets.

@kloon
Last active February 28, 2021 16:21
Show Gist options
  • Save kloon/7511753 to your computer and use it in GitHub Desktop.
Save kloon/7511753 to your computer and use it in GitHub Desktop.
WooCommerce Sort by sales - add an option to your WooCommerce store for sorting products by sales
<?php
// Place the code below in your theme's functions.php file
add_filter( 'woocommerce_get_catalog_ordering_args', 'wc_get_catalog_ordering_args' );
function wc_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 ( 'sales' == $orderby_value ) {
$args['orderby'] = 'meta_value_num';
$args['order'] = 'DESC';
$args['meta_key'] = 'total_sales';
}
return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'wc_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'wc_catalog_orderby' );
function wc_catalog_orderby( $sortby ) {
$sortby['sales'] = 'Sales';
return $sortby;
}
?>
@bhanu2217
Copy link

This code displays out of stock products also

@bhanu2217
Copy link

The code doesn't seem to be working.

@alexapaige
Copy link

Yep, it doesnt work. :( WC 2.1.9 + WP 3.9.1.

@alexapaige
Copy link

Scratch that it does work. :) It's just for the front end, not the back end.

@jaworowicz
Copy link

jaworowicz commented Feb 20, 2019

Works, but not for products with variants

@IW83
Copy link

IW83 commented Nov 30, 2020

This does not work... products who are on sale are not at the top of a product catalog page. Please help...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment