0
votes

I use this code for adding stock options on woocommerce

// New Stock Statuses
function add_custom_stock_type() {
?>
<script type="text/javascript">
jQuery(function(){
    jQuery('._stock_status_field').not('.custom-stock-status').remove();
});
</script>
<?php

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' ),
    'onrequest' => __( 'Product is out of stock but available in physical store.', 'woocommerce' ), // The new option without add to cart button
    'preorder' => __( 'Product can be preordered.', 'woocommerce' ), // The new option that should have add to cart button
), '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->stock_status ) {
    case 'instock':
        $data = array( 'availability' => __( 'In Stock.', 'woocommerce' ), 'class' => 'in-stock' );
    break;
    case 'outofstock':
        $data = array( 'availability' => __( 'Out of Stock.', 'woocommerce' ), 'class' => 'out-of-stock' );
    break;
    case 'onrequest':
        $data = array( 'availability' => __( '', 'woocommerce' ), 'class' => 'on-request' );
        echo '<p class="stock on-request">Out of stock, but in physical store</p>';
    break;
    case 'preorder':
        $data = array( 'availability' => __( '', 'woocommerce' ), 'class' => 'preorder' );
        echo '<p class="stock on-request">Preorder product.</p>';
    break;
}
return $data;
}
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 10, 2);

This code adds additional statuses to the original stock options in WooCommerce, but set the new statuses as out of stock which means it removes the add to cart button on product page.

I’m wondering, though, is there any way to add more stock statuses that are ”in stock”?

I would like to add additional statuses that are out of stock, like now (which removes the add to cart-button) and some that still has the add to cart button.

Is this possible?

2

2 Answers

1
votes

I've been trying to create something like this from the past... And can say that current version of woocommerce does not provide a decent way of adding custom stock status...

However, what's lacking in your code is this...

add_filter('woocommerce_product_is_in_stock', 'woocommerce_product_is_in_stock' );

function woocommerce_product_is_in_stock( $is_in_stock ) {
    global $product;

    // array of custom stock statuses that will have add to cart button
    $stock_statuses = array('onrequest','preorder');

    if (!$is_in_stock && in_array($product->stock_status, $stock_statuses )) {
        $is_in_stock = true;
    }

    return $is_in_stock;
}
0
votes

did you try using Woocommerce Custom Stock plugin? https://wordpress.org/plugins/woo-custom-stock-status/

It helps me to maintain custom stock messages.