Last active
February 28, 2021 16:21
-
-
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
This file contains 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
<?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; | |
} | |
?> |
The code doesn't seem to be working.
Yep, it doesnt work. :( WC 2.1.9 + WP 3.9.1.
Scratch that it does work. :) It's just for the front end, not the back end.
Works, but not for products with variants
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
This code displays out of stock products also