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.

Adding new product status to WooCommerce

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

Leave Comment

One Reply to “Adding new product status to WooCommerce”

  • Genesis Edens says:

    After exploring a number of the blog posts on your web
    site, I truly like your technique of blogging. I saved as a favorite it to my bookmark
    webpage list and will be checking back soon. Take a look at my
    web site too and tell me how you feel.

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