Simon Davies Digital ltd

Show Woocommerce stock status for unmanaged products

Posted by & filed under Woocommerce, WordPress.

The WooCommerce stock status (In Stock, Out of stock) will only show if WooCommerce is managing the stock for you. I was working on a project and the client wanted the ability to manually set if the product was in stock or not. The code snippet below will allow you to show the stock status for both managed and unmanaged products. Just copy and paste into your themes functions.php file.

// Show the status for unmanaged stock products
add_filter( 'woocommerce_get_availability', 'show_stock_status_for_unmanaged_products', 1, 2);
function show_stock_status_for_unmanaged_products( $availability, $_product )
{
	// Don't do anything if managing stock
	if($_product->managing_stock()) return $availability;

	if($_product->is_in_stock()) :
		$availability['availability'] = __('In Stock', 'woocommerce');
		$availability['class'] = '';
	else :
		$availability['availability'] = __('Out of Stock', 'woocommerce');
		$availability['class'] = 'out-of-stock';
	endif;

	return $availability;
}
  • ali

    hi,

    thank you for this code

    i want show stock status in another place of template, What I do?

  • Henkie

    Thanks!

  • Thank you.

  • Great snippet – However is it possible to change the actual message, from “In Stock” to something else – Like “In Stock – Delivery Days 1-2” – I tryed changing it, but then nothing shows up?

  • Guest

    To translate the In Stock or Out of stock:

    $availability[‘availability’] = esc_html( $availability[‘availability’] );

    Very Good Job!!!

  • Debbie Campbell

    This is just what I was looking for – thanks!

  • Amit Ramani

    I had been trying to do this all day. Thank you…this works just as advertised.

  • Vadim Pavlovich

    Thanks!

  • Thanks

  • Jason

    Thanks!