WooCommerce: Sort Out-of-Stock Products Last in Category Lists

Last updated: Omid Bodagh General No comments 1 minute read

To do this, just place the following code in your theme’s functions.php file. It hooks into WooCommerce’s catalog ordering arguments and sorts by stock status, so unavailable products land at the end of the category listing.

add_filter( 'woocommerce_get_catalog_ordering_args', 'mihanwp_sort_by_stock', 9999 );

function mihanwp_sort_by_stock( $args ) {
$args['orderby'] = 'meta_value';
$args['order'] = 'ASC';
$args['meta_key'] = '_stock_status';
return $args;
}

 

I am an SEO specialist who likes helping businesses get seen on Google. From WordPress to SEO, whatever an online business needs to grow, I can help with it.

Leave a comment