Skip to content

Instantly share code, notes, and snippets.

@akshuvo
Forked from saimonh3/class-wc-query.php
Created May 6, 2019 21:38
Show Gist options
  • Save akshuvo/03290b58216f504bf596a790a06e2850 to your computer and use it in GitHub Desktop.
Save akshuvo/03290b58216f504bf596a790a06e2850 to your computer and use it in GitHub Desktop.
Sort product by popularity with price greater than zero
add_action( 'woocommerce_product_query', function( $qeury ) {
if ( empty( $qeury->query_vars['orderby'] ) || 'popularity' !== $qeury->query_vars['orderby'] ) {
return;
}
add_filter( 'posts_clauses', function( $args ) {
$args['orderby'] = ' CASE wc_product_meta_lookup.min_price WHEN 0 THEN 1
ELSE -1
END ASC, wc_product_meta_lookup.total_sales DESC, wc_product_meta_lookup.product_id DESC
';
return $args;
} );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment