Adding new product status to WooCommerce

How to add new product status to WooCommerce

Woocommerce is enough flexible tool, that allow build e-shop for every taste. Besides statuses by default, if it necessary the plugin allow add new. This method will be works for Simple Product type.

Problem: Adding new product status to WooCommerce

Solution: You'll should put next code to functions.php:

<?php
	// Added New Status "Coming Soon" To Stock Status In Product Data
	add_action( 'woocommerce_product_options_stock_status', 'add_custom_stock_type' );

	function add_custom_stock_type() { ?>
    	<script>
			jQuery( function () {
				jQuery( '._stock_status_field' ).not( '.custom-stock-status' ).remove();
			} );
    	</script>
	<?php
		woocommerce_wp_select( [ 'id' => '_stock_status', 'wrapper_class' => 'hide_if_variable custom-stock-status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => [
			'instock' 		=> __( 'In stock', 'woocommerce' ),
			'outofstock' 	=> __( 'Out of stock', 'woocommerce' ),
			'onbackorder' 	=> __( 'On backorder', 'woocommerce' ),
			'comingsoon' 	=> __( 'Coming Soon', 'woocommerce' ),
		], 'desc_tip' => true, 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ) ] );
	}

	add_action( 'woocommerce_process_product_meta', 'save_custom_stock_status', 99, 1 );

	function save_custom_stock_status( $product_id ) {
		update_post_meta( $product_id, '_stock_status', wc_clean( $_POST['_stock_status'] ) );
	}

	add_action( 'woocommerce_get_availability', 'woocommerce_get_custom_availability', 10, 2 );

	function woocommerce_get_custom_availability( $data, $product ) {
		switch ( $product->stock_status ) {
			case 'instock':
				$data = [ 'availability' => __( 'In stock', 'woocommerce' ), 'class' => 'in-stock' ];
				break;
			case 'outofstock':
				$data = [ 'availability' => __( 'Out of stock', 'woocommerce' ), 'class' => 'out-of-stock' ];
				break;
			case 'onbackorder':
				$data = [ 'availability' => __( 'On backorder', 'woocommerce' ), 'class' => 'on-backorder' ];
				break;
			case 'comingsoon':
				$data = [ 'availability' => __( 'Coming Soon', 'woocommerce' ), 'class' => 'coming-soon' ];
				break;
		}
		return $data;
	}