WooCommerce: In Stock Products Shortcode

We’ve already coded a solution to display WooCommerce out of stock products only via a custom shortcode – today we’ll do the opposite: how can we display in stock products only via a handy shortcode?

Once again, the solution “gets” the list of products IDs that are in stock, and then passes the result to the official [products] WooCommerce shortcode, so that we don’t reinvent the wheel.

You can then use the [in_stock_products] shortcode anywhere you wish – on a blog post, in a custom page, in a widget, and so on. You can even customize the output with the official WooCommerce [products] shortcode parameters such as columns, orderby, limit, etc. Enjoy!

On this custom WordPress page, the [in_stock_products] shortcode outputs the list of… in stock products!

PHP Snippet: Shortcode to Display WooCommerce In Stock Products Only

/**
 * @snippet       Display In Stock Products via Shortcode - WooCommerce
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 7
 * @community     https://businessbloomer.com/club/
 */

add_shortcode( 'in_stock_products', 'bbloomer_in_stock_products_shortcode_maybe_cat' );
   
function bbloomer_in_stock_products_shortcode_maybe_cat() {
	
	$args = array(
		'post_type' => 'product',
		'posts_per_page' => -1,
		'post_status' => 'publish',
		'meta_query' => array(
			array(
				'key' => '_stock_status',
				'value' => 'instock',
			)
		),
		'fields' => 'ids',
	);

	$product_ids = get_posts( $args ); 
	$product_ids = implode( ",", $product_ids );

	return do_shortcode("[products ids='$product_ids']");
 
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent 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. Follow @rmelogli

4 thoughts on “WooCommerce: In Stock Products Shortcode

  1. Hello, is it possible to highlight in stock variations on product page, where out of stock variations are available for pre-order? Flatsome theme.

  2. Hi Rodolfo,

    Thank you for all your work. Is there a code to show products marked as “Out of Stock” from the dropdown menu at the WooCmmerce /shop filter where you also see “sort by Latest” and “Sort by Popularity,” etc.?

    Thank you,

    Ozy

    1. Hello Ozy, 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!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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