Change query for out of stock products – WooCommerce


I am using WooCommerce and would would like to show the “out of stock” product last in a custom query on the archive page. How can I do that?

Currently we are making the newest product show first with this custom query:

/* Show latest products first on the category page */
add_action( 'pre_get_posts', 'mik_exclude_category' );
function mik_exclude_category( $query ) {
if ( $query->is_main_query() ) {
$query->set( 'orderby', 'date' );
$query->set( 'order', 'DESC' );

So, we would like to do both. Show the newest products first, but show “out of stock” products last no matter how new they are.