How to add a new stock status to WooCommerce
WooCommerce is a flexible tool that lets you build a shop for every taste. Besides the default stock statuses (In stock, Out of stock, On backorder), it allows adding new ones. Here we add “Coming Soon”. This method works for simple products.
Problem: Add a new stock status, “Coming Soon”, to WooCommerce products.
Solution: WooCommerce takes the list of stock statuses from the woocommerce_product_stock_status_options filter. A status added there appears in the product editor and is saved like the default ones. Then we set the text on the product page and make the product not purchasable.
Put the code into functions.php:
<?php
// 1. Add "Coming Soon" to the stock statuses
add_filter( 'woocommerce_product_stock_status_options', 'add_coming_soon_stock_status' );
function add_coming_soon_stock_status( $statuses ) {
$statuses['comingsoon'] = __( 'Coming Soon', 'theme-name' );
return $statuses;
}
// 2. The availability text and CSS class on the product page
add_filter( 'woocommerce_get_availability_text', 'coming_soon_availability_text', 10, 2 );
function coming_soon_availability_text( $text, $product ) {
return 'comingsoon' === $product->get_stock_status() ? __( 'Coming Soon', 'theme-name' ) : $text;
}
add_filter( 'woocommerce_get_availability_class', 'coming_soon_availability_class', 10, 2 );
function coming_soon_availability_class( $class, $product ) {
return 'comingsoon' === $product->get_stock_status() ? 'coming-soon' : $class;
}
// 3. A "Coming Soon" product cannot be bought yet
add_filter( 'woocommerce_product_is_in_stock', 'coming_soon_not_in_stock', 10, 2 );
function coming_soon_not_in_stock( $is_in_stock, $product ) {
return 'comingsoon' === $product->get_stock_status() ? false : $is_in_stock;
}
The third filter matters: WooCommerce treats every status except “Out of stock” as available, so without it a “Coming Soon” product could be added to the cart.
Now in Product data → Inventory the “Stock status” select has the new option, and the product page shows “Coming Soon” with the class coming-soon for styling.
NOTE: The status is set by hand only when “Manage stock?” is off. With stock management on, WooCommerce calculates the status from the quantity and replaces “Coming Soon” with “In stock” or “Out of stock”. For variable products the status is set per variation.
My first version of this code replaced the “Stock status” field with jQuery and saved it in woocommerce_process_product_meta. The filter above does the same with less code, and WooCommerce validates and saves the value itself.