WooCommerce – Sort Catalog by Stock Status

A quick code snippet to allow you to sort your catalogue products by Instock/Out of stock.

/**
 * @what          Sort Products By Stock Status - WooCommerce Shop
 * @author        Primal Media
 * @compatible    WooCommerce 5+
 */
 
add_filter( 'woocommerce_get_catalog_ordering_args', 'primal_first_sort_by_stock_amount', 9999 );
 
function primal_first_sort_by_stock_amount( $args ) {
   $args['orderby'] = 'meta_value';
   $args['meta_key'] = '_stock_status';
   return $args;
}

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *