Skip to content

Instantly share code, notes, and snippets.

View daveloodts's full-sized avatar

Dave Loodts daveloodts

View GitHub Profile
@daveloodts
daveloodts / gist:bab5f9a00808d5efe90058a7d6beae3a
Created November 22, 2024 16:15
Sort on stock in woocommerce
// Add a new sorting option for pushing out-of-stock products last
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_sort_out_of_stock_last', 9999 );
function custom_sort_out_of_stock_last( $args ) {
// Check if the custom sorting option is selected
if ( isset( $_GET['orderby'] ) && $_GET['orderby'] === 'instock_last' ) {
$args['orderby'] = 'meta_value';
$args['meta_key'] = '_stock_status';
$args['order'] = 'ASC'; // 'instock' comes before 'outofstock'
}