Creating Custom Product Stock Statuses in WooCommerce

In the WooCommerce ecosystem, there are plugins that can implement custom stock status functionality. Sometimes, for specific business logic, this can be extremely useful. In this article, we’ll explore how to achieve this functionality through code.

For example, we’ll create a custom product stock status named “Inquiry.” This status is similar to “Out of Stock”—customers cannot add the product to their cart, but there will be a link pointing to an “Inquiry Page” instead of an “Add to Cart” button.

This custom product stock status will work with standard WooCommerce features (like product filters) and third-party plugins (like inventory sync extensions).

Here are several steps to implement this functionality. Let’s take a look together.

Adding Custom Order Statuses in Product Settings

First, we need to add a product status option on the product edit page. With the help of the woocommerce_product_stock_status_options filter hook, we can easily achieve this.

add_filter( 'woocommerce_product_stock_status_options', 'wprs_product_statuses' );

function wprs_product_statuses( $product_statuses ){

	// We need to add the custom product status in the format slug => name
	$product_statuses[ 'contact-us' ] = 'Contact us';
	// You can also remove some default product stock statuses here if needed

	// Don't forget to return the modified statuses array
	return $product_statuses;
}

Now, you can select the custom stock status in the “Inventory” tab of the product settings:

How to add and select a custom product status

Click the “Update” button directly, and the product status value will be saved. This also applies to product variations.

Making Products with Custom Statuses Non-Purchasable

Now we have the custom product stock status, but its working principle is similar to the “In Stock” status, meaning we can still purchase the product as usual. Since our custom “Inquiry” status requires customers to contact the store owner, it should not be possible to add these products to the cart.

We need to modify this functionality through one of the following two hooks:

  • woocommerce_is_purchasable
  • Or woocommerce_product_is_in_stock

What’s the difference between them?

Simply put, woocommerce_is_purchasable is a strict hook. It assumes the product can never be purchased; if any customer has the product in their cart when you change the status to “Contact Us,” it will be removed immediately. However, the woocommerce_product_is_in_stock hook assumes the product might sometimes be purchasable; the product remains in the customer’s cart, but they will receive a message like this:

Product out of stock error message in the cart in WooCommerce

Therefore, depending on your needs, both hooks work similarly:

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

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

Displaying Custom Messages on the Product Page

Once you’ve completed the previous step, the “Add to Cart” button and quantity selector on the product page will be removed, but we need to display some content, right?

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

function wprs_product_stock_status_text( $text, $product ) {

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

	return $text;

}

Result:

Custom product availability message in WooCommerce

By the way, you can also use the woocommerce_get_availability_class hook to change the CSS class of the wrapping <p> container for this message.

Changing the “Add to Cart” Button on the Archive Page

Here, we can actually do two things:

  1. For example, we can use the woocommerce_product_add_to_cart_text filter hook to change the “Add to Cart” button text to “Inquiry.”
  2. We can also use woocommerce_product_add_to_cart_url to change the button’s URL.

Below is the specific implementation code:

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

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

	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 );

Here’s how our new button looks:

Custom add to cart button in WooCommerce

Managing the Product List Table

Finally, we want to correctly display our custom stock status on the “All Products” page in the WordPress admin.

Custom stock status in WordPress admin product list table

This can also be achieved with a simple woocommerce_admin_stock_html filter hook:

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 );

With the code snippets above, we’ve implemented a simple but complete custom WooCommerce stock status feature. Based on custom stock statuses, we can combine business requirements to implement even more personalized features. If you have any questions about the code mentioned in this article, feel free to ask in the comments!

Related Posts

Leave a Reply

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