Add a stock option to Woocommerce 3.0

I am trying to add in a custom stock_status to woocommerce 3.0 in wordpress.

The end goal is to add a 3rd inventory option on the product edit page, “On Hold”, and display that stock status on the product page.

Previously I was able to use the method here:
Add stock option in woocommerce, which does work to add extra stock options to the product admin, but it looks like with Woocommerce 3.0 there is something over-riding my settings on the actual product page. It was also working on the products page until I upgraded to 3.0.

My functions.php:

function add_custom_stock_type() {
<script type="text/javascript">

woocommerce_wp_select( array( 'id' => '_stock_status', 'wrapper_class' => 'hide_if_variable custom-stock-status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
    'instock' => __( 'In stock', 'woocommerce' ),
    'outofstock' => __( 'Out of stock', 'woocommerce' ),
    'onhold' => __( 'On Hold', 'woocommerce' ), // The new option !!!
), '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_product_options_stock_status', 'add_custom_stock_type');

function save_custom_stock_status( $product_id ) {
update_post_meta( $product_id, '_stock_status', wc_clean( $_POST['_stock_status'] ) );
add_action('woocommerce_process_product_meta', 'save_custom_stock_status',99,1);

function woocommerce_get_custom_availability( $data, $product ) {
switch( $product->get_stock_status ) {
    case 'onhold':
        $data = array( 'availability' => __( 'On Hold', 'woocommerce' ), 'class' => 'on-hold' );
    case 'instock':
        $data = array( 'availability' => __( 'In stock', 'woocommerce' ), 'class' => 'in-stock' );
    case 'outofstock':
        $data = array( 'availability' => __( 'Out of stock', 'woocommerce' ), 'class' => 'out-of-stock' );
return $data;
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 3, 2);

I can see in the DB that the stock_status is properly setting to “On Hold”, my custom status, when I select it on the admin, but this is not being applied on the actual products page.

To verify, I modified my price.php to simply output the stock status:

<p class="price">

    $stockamount = $product->get_stock_quantity();
    $price = $product->get_price_html();
    $stockstatus = $product->get_stock_status();
    $pricelabelone = "Out of Stock";
    $pricelabeltwo = "On Hold";

     echo $stockstatus;            

However, even though I set products to “On Hold” (and this is saving), the product page is always outputting “In Stock” (test product:

What am I missing?

Read more here: Add a stock option to Woocommerce 3.0

Leave a Reply

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