Create a Custom Stock Status

In this tutorial I am going to show you how you can create a custom stock status for your WooCommerce products. There are a bunch of plugins over there, which allow you to implement similar functionality in one way or another, but why to use a plugin when can do exactly the same with a couple of lines of code in your theme. But of course, it depends.

As an example here we will create a “Contact us” custom product stock status, which will be kind of similar to “Out of stock” – customers won’t be able to add a product to a cart, but there is going to be a link to a contact page instead of an “Add to cart” button.

This custom product stock status will work with both standard WooCommerce features (like product filters) and third party plugins (my Duplicate SKU stock sync plugin for example).

And now let’s break down the whole process into steps.

Add a Custom Order Status into Product Settings

First of all we need to add an option on the edit product page into a product status selection and we can easily do that with the help of the woocommerce_product_stock_status_options filter hook.

add_filter( 'woocommerce_product_stock_status_options', 'rudr_product_statuses' );

function rudr_product_statuses( $product_statuses ){

	// let's add our custom product status in a format slug => name
	$product_statuses[ 'contact-us' ] = 'Contact us';
	// you can also remove some of the default product stock statuses by the way

	// don't forget to return the changed array of statuses
	return $product_statuses;
}

Now our custom product status can be selected in the product settings, “Inventory” tab:

How to add and select a custom product status

Even if you hit the “Update” button, the product status value is going to be saved. And it also works for product variations.

Make Products with a Custom Product Status Non-purchasable

Ok, now we have our custom product stock status, but it works kind of similar to the “In stock” status, so we can purchase a product as usual and so on.

Since our custom “Contact us” stock status requires the customer to contact a store owner, obviously it shouldn’t be possible to add a product with this status to the cart and purchase it.

How we can do it? Actually I can suggest you one of the following hooks:

  • either woocommerce_is_purchasable
  • or woocommerce_product_is_in_stock,

but what is the difference between them?

I can say that woocommerce_is_purchasable is a more strict version of the hook, it assumes that a product can never be purchased, if any customer has this product in the cart, when you change the stock status to “contact us”, it will be removed immediately. But the hook woocommerce_product_is_in_stock assumes, that sometimes this product can be purchased, your customer products will remain in the cart and they will get a message like that:

Product out of stock error message in the cart in WooCommerce

So it is up to you to decide, both of the hooks work the similar way:

add_filter( 'woocommerce_product_is_in_stock', 'rudr_allow_purchase', 10, 2 );
//add_filter( 'woocommerce_is_purchasable', 'rudr_allow_purchase', 10, 2 );

function rudr_allow_purchase( $allow, $product ) {
	
	if( 'contact-us' === $product->get_stock_status() ) {
		$allow = false;
	}
	
	return $allow;
}

Display a Custom Message on the Product Page

Once we implemented the previous step, the add to cart button and a quantity selection will be removed from the product page, but we need to display something instead, right?

add_filter( 'woocommerce_get_availability_text', 'rudr_product_stock_status_text', 10, 2 );

function rudr_product_stock_status_text( $text, $product ) {

	if( 'contact-us' === $product->get_stock_status() ) {
		$text = sprintf(
			'This product is only available on request, please <a href="%s">contact us</a>',
			site_url( 'contact-us' )
		);
	}

	return $text;

}

The result:

Custom product availability message in WooCommerce

By the way, you can also change the CSS-class of a wrapping container <p> of this message with the hook woocommerce_get_availability_class.

Change “Add to cart” Buttons on the “Shop” Page

Here we can actually do two things:

  1. We can change the text of “Add to cart” buttons to “Contact us” for example, with the woocommerce_product_add_to_cart_text filter hook.
  2. And we can also change buttons URL with the woocommerce_product_add_to_cart_url.

Now let’s do it.

add_filter( 'woocommerce_product_add_to_cart_text', function( $text, $product ) {

	if( 'contact-us' === $product->get_stock_status() ) {
		$text = 'Contact us';
	}

	return $text;

}, 25, 2 );

add_filter( 'woocommerce_product_add_to_cart_url', function( $url, $product ) {

	if( 'contact-us' === $product->get_stock_status() ) {
		$url = site_url( 'contact-us' );
	}

	return $url;

}, 25, 2 );

That’s how our brand new “Add to cart” button is going to look like:

Custom add to cart button in WooCommerce

Admin Product List Table

And the last but not the least, we’re going to make our custom stock status to be displayed correctly on Products > All products page in WordPress admin.

Custom stock status in WordPress admin product list table

It can be done with a simple woocommerce_admin_stock_html filter hook as well:

add_filter( 'woocommerce_admin_stock_html', function( $stock_html, $product ) {

	if( 'contact-us' === $product->get_stock_status() ) {
		$stock_html = '<mark class="onbackorder">On request</mark>';
	}

	return $stock_html;

}, 25, 2 );
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X