WooCommerce: Show In Stock Products First @ Shop

We’ve already seen how to add a custom “Product Sorting” option to the “Default Sorting” dropdown in the Shop page.

The task I was presented with, however, was to display items based on a custom “meta key”. Now, if you have no idea what a “meta key” is, don’t worry too much. For example, “stock status” is one of these keys, and therefore you can sort products by that.

Anyway, let’s abandon the Japanese for now (hey, unless you speak Japanese!) and get into the coding – as usual, simply copy/paste this little plugin in your functions.php and you’ll get the wanted result ๐Ÿ™‚

WooCommerce: sort products by stock (or any other custom meta key)

PHP Snippet 1: Sort Products by Stock Status @ WooCommerce Shop


/**
 * @snippet       Sort Products By Stock Status - WooCommerce Shop
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=79629
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.2
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'woocommerce_product_query', 'bbloomer_sort_by_stock_status_then_alpha', 999 );

function bbloomer_sort_by_stock_status_then_alpha( $query ) {
    if ( is_admin() ) return;
    $query->set( 'meta_key', '_stock_status' );
    $query->set( 'orderby', array( 'meta_value' => 'ASC' ) );
}

PHP Snippet 2: Sort Products SHORTCODE by Stock Status


/**
 * @snippet       Sort Products Shortcode By Stock Status
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=79629
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.2
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

// Note: products shortcode docs https://docs.woocommerce.com/document/woocommerce-shortcodes/#section-6
// In order for the snippet to work, you must add the parameter orderby = "stock" to the shortcode
// You can add more parameters to it of course e.g. limit, paginate, etc. It's all in the docs

add_filter('woocommerce_shortcode_products_query', 'bbloomer_sort_by_stock_status_shortcode', 999, 3);

function bbloomer_sort_by_stock_status_shortcode( $args, $atts, $type ) {
    if ( $atts['orderby'] == "stock" ) {
        $args['orderby']  = array( 'meta_value' => 'ASC' );
        $args['meta_key'] = '_stock_status';
    }
    return $args;
}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7+.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce Customization?

Check out these free video tutorials. You can start learning how to customize WooCommerce without unnecessary plugins. Watch me code and learn by example!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

16 thoughts on “WooCommerce: Show In Stock Products First @ Shop

  1. Hi,
    I’m sorry about my English.
    I used your code and it works correctly.
    But now I have to change the order.
    Is there a possibility to order products for the best sellers and put out of stock products at the bottom of the page?

    Thank you
    Mirco

    1. Hi Mirco, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. Good day.
    I do everything according to your instructions.
    But I can not. It is necessary that the goods be sorted by quantity in stock. First of all, cho would show the goods of which are more in stock.
    Help please solve this problem.
    WordPress 5.2
    WooCommerce 3.6.2

    1. Hi Roman, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Great snippet. How about one which sorts by in stock variables on the single product page ?? So when you 1st land on the page the first available in stock item is ready to buy instead of if the default item is out of stock?

    1. Hello Wayne, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  4. Hi there. We like this code and want to use it, but we also want to enable it so that ‘stock’ sorts it first by stock, then by title.
    So the page loads in Title order, but with ‘out of stock’ at the end of the pagination.

    I don’t see a way to do this, as ‘title’ isn’t a meta_value, like price and SKU are.

    Can you help?

    1. Hey Simon, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  5. Hi Rodolfo,

    Thank you again for all your work and support to the community.

    This code works wells. However it messes up the order of certain other places. For example, all Woo sites has the /shop/?orderby=date function, which we use to show the new arrivals in our store. But after adding this code, the new arrival products via the above link I pointed out is all a mess. It’s not showing the latest product which was added to the store anymore.

    Would appreciate if your feedback in this matter ๐Ÿ™‚

    Thanks again!

    1. Hey Randika, thanks for your comment! You’ll need to either run this snippet just on the pages you want, or otherwise customize the coding to make it work with the other sorting options ๐Ÿ™‚

  6. Hello,
    Is this possible to make the code work on single product page in Related products section?

    I mean – now my Related products section show often out of stock products and I would like to get rid of them or shift them to the end of the list (if there are only a few related products).

    Regards,
    Domin

    1. Hello Domin, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  7. Kinda works, but then you lose the alphanumeric sorting, so the “in stock” items are all at top, but no longer listed alphabetically. This outcome is on current WP & Woo as of November 2018.

    1. Hey Matt, thanks for your comment! I guess you can add an “orderby” array element and make that happen – let me know ๐Ÿ™‚

  8. Hi.
    How can I show products by two model:
    first: Stock Ascending
    second: publish date Descending
    Thanks

    1. Hello Ushina, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

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