How to Customize Woocommerce Stock Status? (17 hacks)

Let’s take a look at how to customize Woocommerce stock status without any fancy plugin. Pay attention that all the snippets shown below should be added to your child theme’s functions.php file or better yet, use a snippet manager like Code Snippets or WpCodeBox (my favorite). If you’re interested, then grab WPCodeBox with a nice 20% discount here (SAVE 20% Coupon WPSH20).

Video: How to Customize Woocommerce Stock Status?

Maybe some parts of the snippets are difficult to understand. If this is the case then take a look at the on how to customize Woocommerce stock status.

How to add custom stock status to products in WooCommerce?

Woocommerce comes with default stock statuses “in stock”, “out of stock” and “available on backorder”. If you would like to add custom stock status to products in WooCommerce then use this snippet here below.

This snippet here below adds four additional stock statuses to Woocommerce.

  • Pre order
  • Due in stock in 14 days
  • Due in stock in 21 days
  • Due in stock in 31 days

This is the end result.

How to add custom stock status to products in WooCommerce?

Pay attention that in order to add or remove your custom stock statuses you need to remove some lines accordingly. Take a look at the video above that shows how to do that (see video starting at 2:10) .

// Add custom stock status to products in WooCommerce
function filter_woocommerce_product_stock_status_options( $status ) {
    // Add new statuses
    $status['pre_order'] = __( 'Pre order', 'woocommerce' );
    $status['due_in_14'] = __( 'Due in stock in 14 days', 'woocommerce' );
  	$status['due_in_21'] = __( 'Due in stock in 21 days', 'woocommerce' );
    $status['due_in_31'] = __( 'Due in stock in 31 days', 'woocommerce' );
    return $status;
}
add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );

// Availability text
function filter_woocommerce_get_availability_text( $availability, $product ) {
    // Get stock status
    switch( $product->get_stock_status() ) {
        case 'pre_order':
            $availability = __( 'Pre order', 'woocommerce' );
        break;
        case 'due_in_14':
            $availability = __( 'Due in stock in 14 days', 'woocommerce' );
        break;
		case 'due_in_21':
            $availability = __( 'Due in stock in 21 days', 'woocommerce' );
        break;
		case 'due_in_31':
            $availability = __( 'Due in stock in 31 days', 'woocommerce' );
        break;
    }

    return $availability; 
}
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );

// Availability CSS class
function filter_woocommerce_get_availability_class( $class, $product ) {
    // Get stock status
    switch( $product->get_stock_status() ) {
        case 'pre_order':
            $class = 'pre-order';
        break;
        case 'due_in_14':
            $class = 'due-in-14';
        break;
		case 'due_in_21':
            $class = 'due-in-21';
        break;
		case 'due_in_31':
            $class = 'due-in-31';
        break;
    }

    return $class;
}
add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 );

// Admin stock html
function filter_woocommerce_admin_stock_html( $stock_html, $product ) {
    // Simple
    if ( $product->is_type( 'simple' ) ) {
        // Get stock status
        $product_stock_status = $product->get_stock_status();
    // Variable
    } elseif ( $product->is_type( 'variable' ) ) {
        foreach( $product->get_visible_children() as $variation_id ) {
            // Get product
            $variation = wc_get_product( $variation_id );
            
            // Get stock status
            $product_stock_status = $variation->get_stock_status();
            
            /*
            Currently the status of the last variant in the loop will be displayed.
            
            So from here you need to add your own logic, depending on what you expect from your custom stock status.
            
            By default, for the existing statuses. The status displayed on the admin products list table for variable products is determined as:
            
            - Product should be in stock if a child is in stock.
            - Product should be out of stock if all children are out of stock.
            - Product should be on backorder if all children are on backorder.
            - Product should be on backorder if at least one child is on backorder and the rest are out of stock.
            */
        }
    }
    
    // Stock status
    switch( $product_stock_status ) {
        case 'pre_order':
            $stock_html = '<mark class="pre-order" style="background:transparent none;color:#33ccff;font-weight:700;line-height:1;">' . __( 'Pre order', 'woocommerce' ) . '</mark>';
        break;
        case 'due_in_14':
            $stock_html = '<mark class="due-in-14" style="background:transparent none;color:#666;font-weight:700;line-height:1;">' . __( 'Due in stock in 14 days', 'woocommerce' ) . '</mark>';
        break;
		case 'due_in_21':
            $stock_html = '<mark class="due-in-21" style="background:transparent none;color:#2a2a2a;font-weight:700;line-height:1;">' . __( 'Due in stock in 21 days', 'woocommerce' ) . '</mark>';
        break;
		case 'due_in_31':
            $stock_html = '<mark class="due-in-31" style="background:transparent none;color:#2a2a2a;font-weight:700;line-height:1;">' . __( 'Due in stock in 31 days', 'woocommerce' ) . '</mark>';
        break;
    }
 
    return $stock_html;
}
add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 );

How to gray out Woocommerce of stock variations?

Currently, if the variation is out of stock, then it still appears in the dropdown menu. So, customers have to make a click in order to see that the variation is out of stock.

If you would like to gray out Woocommerce out of stock variations (not possible to select), then use this snippet here below.?

How to gray out Woocommerce of stock variations?
// Gray out Woocommerce of stock variations
add_filter( 'woocommerce_variation_is_active', 'wpsh_disable_outofstock_variations', 10, 2 );
function wpsh_disable_outofstock_variations( $is_active, $variation ) {
    if ( ! $variation->is_in_stock() ) return false;
    return $is_active;
}

Woocommerce: How Display Contact Form Only When Product is Out Of Stock?

Next hack is especially useful if you would like your customers to contact you when the product is out of stock. So, this snippet here below allows you to display contact form only when Woocommerce product is out of stock.

/* Display contact form instead of "Out Of Stock" message */
add_action('woocommerce_single_product_summary', 'out_of_stock_show_form', 20);
function out_of_stock_show_form() {
    global $product;
    if(!$product->is_in_stock( )) {
        echo  '<div class="your-form">';
        echo  'Unfortunately this product is out of stock.<br>Contact us for more information';
        echo  do_shortcode('[fluentform id="1"]'); // Replace with your own contact form shortcode
        echo  '</div>';
    }
}

/* This will display your contact form when the variation is out of stock */
add_filter( 'woocommerce_available_variation', 'variation_out_of_stock_show_form', 10, 3 );
function variation_out_of_stock_show_form( $data, $product, $variation ) {
    if( ! $data['is_in_stock'] )
    {
        $data['availability_html'] = '<div class="your-form">';
        $data['availability_html'] .= 'Unfortunately this product is out of stock.<br>Contact us for more information';
        $data['availability_html'] .= do_shortcode('[fluentform id="1"]'); // Replace with your own contact form shortcode
        $data['availability_html'] .= '</div>';
    }
    return $data;
}

How to hide Woocommerce product price if product is out of stock?

Now let’s take a loot at how to hide Woocommerce product price if product is out of stock. Snippet below is here to rescue.

// Hide Woocommerce product price if product is out of stock
add_filter( 'woocommerce_get_price_html', 'wpsh_hide_price_for_outofstock', 9999, 2 );
 
function wpsh_hide_price_for_outofstock( $price, $product ) {
   if ( is_admin() ) return $price; // BAIL IF BACKEND
   if ( ! $product->is_in_stock() ) {
      $price = apply_filters( 'woocommerce_empty_price_html', '', $product );
   }
   return $price;
}

How to add suffix to Woocommerce stock quantity?

This snippet is useful if you need to display suffix (kg, mm, cm etc.) next to the quantity. So, let’s add suffix to Woocommerce stock quantity. (change “kg” in the code accordingly).

If you would like to display this conditionally for specific categories or products, then WpCodeBox plugin allows you to do that. See my video above starting from 7:51 mark.

How to add suffix to Woocommerce stock quantity?
// Add suffix to Woocommerce stock quantity?
add_filter( 'woocommerce_format_stock_quantity', 'wpsh_stock_suffix', 9999, 2 );
 
function wpsh_stock_suffix( $stock_quantity, $product ) {
    $stock_quantity .= ' kg'; // change accordingly
    return $stock_quantity;
}

How to change “Read more” text for Woocommerce our of stock products?

By default Woocommerce displays “Read more” on archive pages for out of stock products. With this hack here below we’re going to change it and display “Out of stock” text instead.

So, let’s see how to change “Read more” text for Woocommerce out of stock products.

// Change “Read more" text for Woocommerce our of stock products
add_filter( 'woocommerce_product_add_to_cart_text', 'wpsh_rename_readmore' );
  
function wpsh_rename_readmore( $text ) {
   global $product;       
   if ( $product && ! $product->is_in_stock() ) {           
      return 'Out of stock';
   } 
   return $text;
}

PS! Change the text as you like inside the code (see line 7).

How to change “Out of stock” text on Woocommerce single product pages?

Previous snippet changed “Read more” text for Woocommerce our of stock products on archive pages. This snippet here below changes “Out of stock” text on Woocommerce single product pages.. Instead “Out of stock” we’re going to show “Sold out” (see line 7 inside code).

// Change “Out of stock" text on Woocommerce single product pages
add_filter( 'woocommerce_get_availability', 'change_out_of_stock_text_woocommerce', 1, 2);

function change_out_of_stock_text_woocommerce( $availability, $product_to_check ) {
    // Change Out of Stock Text
    if ( ! $product_to_check->is_in_stock() ) {
        $availability['availability'] = __('Sold Out', 'woocommerce');	
    }
    return $availability;
}

How to change “XX in stock” text on Woocommerce single product pages?

If you would like instead of “10 in stock” display “Quantity available: 1″ then this snippet here below allows you to change “XX in stock” text on Woocommerce single product pages.

// Change “XX in stock" text on Woocommerce single product pages
add_filter( 'woocommerce_get_availability_text', 'wpsh_custom_availability', 99, 2 );
  
function wpsh_custom_availability( $availability, $product ) {
   $stock = $product->get_stock_quantity();
   if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Quantity available: ' . $stock;
   return $availability;
}

How to display Woocommerce stock status on shop/archive pages?

By default Woocommerce doesn’t display stock status on shop/archive pages. We’ll change this with this piece of code.

Display stock status on Woocommerce shop/archive pages
// Display stock status on Woocommerce shop/archive pages
//* Enqueue scripts
add_action( 'wp_enqueue_scripts', 'wpsh_stock_status_archive', 11 );
function wpsh_stock_status_archive() {

	// Activate clip styles
	wp_enqueue_style(  'wpsh-stock-status-archive-style', 
						plugins_url( 'clip-style.css', __FILE__ ), array(),
						'1.0.0' 
	);
}

//* Add stock status to archive pages
add_action( 'woocommerce_after_shop_loop_item', 'wpsh_add_stock_status_archive', 3 );
function wpsh_add_stock_status_archive() {

    global $product;
	$availability = $append = null;

	// Add status for single products
	if( $product->is_type( 'simple' ) ) {

		$availability = $product->get_availability();
		$class = $availability[ 'class' ];
		$output = $availability[ 'availability' ];
	}

	// Add status for variable products
	elseif( $product->is_type( 'variable' ) ) {

		$status = array();

		// Get status class for each variation
		foreach ( $product->get_children() as $child_id ) {
			
				$variation = $product->get_child( $child_id );
				$availability = $variation->get_availability();
				
				// Abandon if stock management is disabled on any variation
				if( ! array_filter( $availability ) )
					return;

				$status[] = $availability[ 'class' ];
		}

		/**
		 * Compile final output and class based on
		 * availability classes set by WooCommerce
		 */
		if( in_array( 'in-stock', $status ) ) {
			$output = __( 'In stock', 'wp-clips' );
			$class = 'in-stock';
		}
		elseif( in_array( 'available-on-backorder', $status ) ) {
			$output = __( 'Available on backorder', 'wp-clips' );
			$class = 'available-on-backorder';
		}
		elseif( in_array( 'out-of-stock', $status ) ) {
			$output = __( 'Out of stock', 'wp-clips' );
			$class = 'out-of-stock';
		}

		// Append output if some items out of stock or available on backorder
		if( ( in_array( 'available-on-backorder', $status ) && $class == 'in-stock' ) ||
			( in_array( 'out-of-stock', $status ) && $class != 'out-of-stock' ) )
			$append = ' ' . __( '(some items)', 'wp-clips' );
	}

	// Output only if set 
	if( isset( $availability ) ){
		echo '<span class="archive-stock ' . esc_attr( $class ) . '">' . esc_html( $output ) . esc_html( $append ) . '</span>';	
	}
}

How to display Woocommerce variations stock on shop pages?

Next I’ll show you how to display Woocommerce variations stock on shop pages. Just use this snippet here below.

// Display Woocommerce variations stock on shop pages
add_action( 'woocommerce_after_shop_loop_item', 'wpsh_vartiations_stock' );
function wpsh_vartiations_stock(){
    global $product;
    if ( $product->get_type() == 'variable' ) {
        foreach ( $product->get_available_variations() as $key ) {
            $variation = wc_get_product( $key['variation_id'] );
            $stock = $variation->get_availability();
            $stock_string = $stock['availability'] ? $stock['availability'] : __( 'In stock', 'woocommerce' );
            $attr_string = array();
            foreach ( $key['attributes'] as $attr_name => $attr_value ) {
                $attr_string[] = $attr_value;
            }
            echo '<br/>' . implode( ', ', $attr_string ) . ': ' . $stock_string;
        }
    }
}

How to change Woocommerce add to cart button text if product is avaliable on backorder?

With the help of this snippet we’re going to change Woocommerce add to cart button text if product is available on backorder. That is, instead of “Add to cart” we’re going to show “Pre-order”.

How to change Woocommerce add to cart button text if product is avaliable on backorder?
// Changes Woocommerce Single product page add to cart button only text if product is in backorder
add_filter( 'woocommerce_product_single_add_to_cart_text', 'wpsh_rename_backorder_button_single_product', 10, 2 );
function wpsh_rename_backorder_button_single_product( $text, $product ){
	if ( $product->is_on_backorder( 1 ) ) {
		$text = __( 'Pre-order', 'woocommerce' );
	}
	return $text;
}

// Changes Woocommerce category page add to cart button only text if product is in backorder
add_filter( 'woocommerce_product_add_to_cart_text', 'wpsh_rename_backorder_button_shop_page', 10, 2 );
function wpsh_rename_backorder_button_shop_page( $text, $product ){
    if ( $product->is_on_backorder( 1 ) ) {
        $text = __( 'Pre-order', 'woocommerce' );
    }
    return $text;
  }

How to change Woocommerce “Avaliable on backorder” text?

This snippet allows you to change Woocommerce “Available on backorder” text in a way that instead of “Available on backorder” you’ll display “Estimated shipping time: 2 weeks”

// Change Woocommerce “Avaliable on backorder" text
add_filter( 'woocommerce_get_availability_text', 'wpsh_change_backorder_text', 10, 2 );
function wpsh_change_backorder_text( $availability_text, $product ) {
    // Check if product status is on backorder
    if ($product->get_stock_status() === 'onbackorder') {
        $availability_text = __( '<div class="woocommerce-message">Estimated shipping time: 2 weeks</div>', 'your-text-domain' );
    }
    return $availability_text;
}

How to display Woocommerce backorder text value from custom field?

Next one is a nifty one. We’re going to:

  • Create a custom field, and
  • Display it under Inventory tab, and
  • Allow to enter custom text inside the text field, and
  • Display the value on Woocommerce single product page only if
  • the stock status is “Available on backorder”

So, let’s see how to display Woocommerce backorder text value from custom field.

// Display Woocommerce backorder text value from custom field
// Add custom field under inventory tab
function wpsh_backorder_custom_field() {
    woocommerce_wp_text_input( array(
        'id'            => '_backorder_info',
        'label'         => __( 'Backorder info', 'woocommerce' ),
        'description'   => __( 'Enter your estimated shipping time for backorders', 'woocommerce' ),
        'desc_tip'      => 'true',
        'type'          => 'text'
    ) );
}
add_action( 'woocommerce_product_options_inventory_product_data', 'wpsh_backorder_custom_field' );

// Save custom field value in database
function wpsh_backorder_custom_field_object( $product ) {
    // Isset
    if ( isset( $_POST['_backorder_info'] ) ) {        
        // Update
        $product->update_meta_data( '_backorder_info', sanitize_text_field( $_POST['_backorder_info'] ) );
    }
}
add_action( 'woocommerce_admin_process_product_object', 'wpsh_backorder_custom_field_object', 10, 1 );

// Display Woocommerce backorder text value from custom field
add_filter( 'woocommerce_get_availability', 'wpsh_custom_on_backorder_text', 10, 2 );

function wpsh_custom_on_backorder_text( $availability, $product ) {
$backorder_text = get_post_meta( $product->get_id(), '_backorder_info', true );

if( $availability['class'] === 'available-on-backorder' && ! empty( $backorder_text ) )
    $availability['availability'] = $backorder_text;

return $availability;
}
How to display Woocommerce backorder text value from custom field?
How to Customize Woocommerce Stock Status? (17 hacks)

How to customize Woocommerce “Available on backorder” text style?

As you see from the screenshot above “Available on backorder” text looks a bit different than it does by default. You can customize Woocommerce “Available on backorder” text style easily by adding this piece of CSS inside Appearance >> Customizer >> Additional CSS code box.

.available-on-backorder {
	  background: #f9f9f9;
    text-align: center;
    padding: 20px;
    border: 1px solid #ebebeb;
    font-weight: 500;
}

How to display Woocommerce backorder notification on cart and checkout page?

If you would like to display Woocommerce backorder notification on cart and checkout page, then use this snippet here below.

How to display Woocommerce backorder notification on cart and checkout page?
// Display Woocommerce backorder notification on checkout page
add_action( 'woocommerce_before_checkout_form', 'wpsh_display_backorder_checkout_notice' );
function wpsh_display_backorder_checkout_notice() {
    $found = false;
    foreach( WC()->cart->get_cart() as $cart_item ) {
        if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
            $found = true;
            break;
        }
    }
    if( $found ) {
// Change this text here. If you want it to show as default Woocommerce message notification then replace 'error' with 'notice'
        wc_print_notice( __("<strong>You have products in the cart that are available only in backorder.</strong><br> For those products estimated delivery time is 2-3 weeks.", "woocommerce"), 'error' ); 
    }
}

// Display Woocommerce backorder notification on cart page
add_action( 'woocommerce_before_cart_table', 'wpsh_display_backorder_cart_notice' );
function wpsh_display_backorder_cart_notice() {
    $found = false;
    foreach( WC()->cart->get_cart() as $cart_item ) {
        if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
            $found = true;
            break;
        }
    }
    if( $found ) {
// Change this text here. If you want it to show as default Woocommerce message notification then replace 'error' with 'notice'
        wc_print_notice( __("<strong>You have products in the cart that are available only in backorder.</strong><br> For those products estimated delivery time is 2-3 weeks.", "woocommerce"), 'error' ); 
    }
}

How to disable Woocommerce payment gateways for bacordered products?

Take a look at this snippet here below. This one will hide BACS and Paypal payment methods if cart contains a backordered product. Lines 9 and 10 are the ones to modify accordingly. Also, if you would like to disable your own custom payment Woocommerce gateways for backordered products then see my video above (watch at 18:42 mark).

// Disable Woocommerce payment gateways for bacordered products
add_filter( 'woocommerce_available_payment_gateways', 'wpsh_backordered_hide', 90, 1 );
function wpsh_backordered_hide( $available_gateways ) {
    if ( is_admin() )
        return $available_gateways;
    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item ){
        if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
            unset($available_gateways['bacs']); // Hide BACS payment gateway
			unset($available_gateways['paypal']); // Hide Paypal payment gateway
            break; // Stop the loop
        }
    }

    return $available_gateways;
}

How to display Woocommerce stock status in cart page?

This one is an easy one. If you would like to display Woocommerce stock status in cart page then use this piece of code.

// Display stock status in cart page
add_action( 'woocommerce_after_cart_item_name', 'wpsh_display_stock_in_cart', 9999, 2 );
 
function wpsh_display_stock_in_cart( $cart_item, $cart_item_key ) {
   $product = $cart_item['data'];
   if ( $product->backorders_require_notification() && $product->is_on_backorder( $cart_item['quantity'] ) ) return;   
   echo wc_get_stock_html( $product );
}

How to display Woocommerce out of stock products last on Shop/Archive pages?

Last snippet of this post allows you to display Woocommerce out of stock products last on Shop/Archive pages.

// Display Woocommerce out of stock products last on Shop/Archive pages
add_action( 'woocommerce_product_query', 'wpsh_out_of_stock_last', 999 );
 
function wpsh_out_of_stock_last( $query ) {
    if ( is_admin() ) return;
    $query->set( 'meta_key', '_stock_status' );
    $query->set( 'orderby', array( 'meta_value' => 'ASC' ) );
}

So, there you go. Now you know how to customize Woocommerce stock status with the help of these 17 hacks.

Useful Woocommerce tips

Here are some of my favorite Wordpress tools

Thank you for reading this article. I hope you found it helpful as you build your own websites and e-commerce sites. Here are some tools I use as a Wordpress developer and enthusiast that I hope you’ll also find helpful.

These are affiliate links, so if you do decide to use any of them, I’ll earn a commission and this helps me create these tutorials and make Youtube videos. But in all honesty, these are the exact tools that I use and recommend to everyone, even my friends and family.

Themes: For the last couple of years I have two go-to themes which I use for every kind of projects. Those two themes are Blocksy theme and Kadence Theme. On this site and my Youtube channel you’ll see a lot of tutorials I have made about them. If you would like to get a 10% discount for both of them then:

Code Snippets manager: WPCodeBox allows you to add code snippets to your site. Also, it allows you to build and manage your WordPress Code Snippets library in the cloud. You can grab it with the 20% discount here (SAVE 20% Coupon: WPSH20).

Contact forms: There are hundreds of contact forms out there but Fluent Forms is the one I like the most. If you need a 20% discount then use this link (save 20% coupon is WPSH20).

Gutenberg add-ons: If I need a good Gutenberg blocks add-on then Kadence Blocks is the one I have used the most. You’ll get 10% discount with the coupon SIMPLEHACKS here.

Website migration: While building a website you probably need a good plugin that can help you with the migration, backups, restoration and staging sites. Well, WpVivid is the one I have used for the last couple of years. If you use this link along with the WPSH20 coupon you’ll get 20% discount.

Woocommerce extensions: There are a bunch of Woocommerce extensions that I really like but the one that stands really out is Advanced Dynamic Pricing. Once again, you’ll get a 20% discount if you use this link here (save 20% coupon is WPSH20)

Web Hosting: If you would like to have a really fast and easy to use managed cloud hosting, then I recommend Verpex Hosting (see my review here). Btw, this site is hosted in Verpex.)

To see all my of most up-to-date recommendations, check out this resource that I made for you!

Do you want to thank me and buy me a beer?

Every donation is entirely welcome but NEVER required. Enjoy my work for free but if you would like to thank me and buy me a beer or two then you can use this form here below.

Choose amount

Best selling plugins

Janek T.
Janek T.

I am a Wordpress enthusiast who has been making websites since 2011. In this site I am offering simple to follow tips on how to use Wordpress and Woocommerce.
If you want to be the first to be notified about the new tutorials then please subscribe to my Youtube channel here
Follow me in Twitter here

Articles: 89