9

I would like to add a new option to the dropdown list of stocks options for a product. By default, there is "Out of stock", "In stock" and I would like to add a third option.

I found the method that displays the dropdown ( in class-wc-meta-box-product-data.php )

    // Stock status
    woocommerce_wp_select( array( 'id' => '_stock_status', 'wrapper_class' => 'hide_if_variable', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
        'instock' => __( 'In stock', 'woocommerce' ),
        'outofstock' => __( 'Out of stock', 'woocommerce' )
    ), 'desc_tip' => true, 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ) ) );

    do_action( 'woocommerce_product_options_stock_status' );

But I don't want to edit Woocommerce class directly, so that we can update Woocommerce without losing any custom code. Is there a way to override this method ?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Laila
  • 1,421
  • 3
  • 13
  • 27
  • Sorry but what you are looking for is not possible without altering the core files, the changes to which will be lost on upgrade. https://support.woothemes.com/hc/communities/public/questions/202868126-Adding-a-stock-choice – Anand Shah Nov 13 '14 at 16:44
  • I've already seen this link but I was wondering if there were an other solution. You can check my own answer below. – Laila Nov 13 '14 at 19:35

2 Answers2

15

for anyone interested, here is complete solution, based on Laila's approach. Warning! My solution is intended to work only with WooCommerce "manage stock" option disabled! I am not working with exact amounts of items in stock. All code goes to functions.php, as usual.

Back-end part

Removing native stock status dropdown field. Adding CSS class to distinguish my new custom field. Dropdown has now new option "On Request".

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' => __( 'On Request', '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');

Sadly, WooCommerce will save only "instock" or "outofstock" values with its native functions. So after all product data processing, I have to re-save my stock status again.

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

Template part

And the last thing - I have to alter data returned by product get_availability() function. When "managing stock" is off, WooCommerce only knows "instock" and "outofstock" values, again. So I have check stock status on my own.

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' => __( 'On request', 'woocommerce' ), 'class' => 'on-request' );
        break;
    }
    return $data;
}
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 10, 2);

Maybe it's not bulletproof solution ... I will update it, eventually.

Marek
  • 588
  • 5
  • 12
  • This worked for me thanks. Got the options in stock, out of stock, 1-3 days and 3-5 days. – Heis May 06 '15 at 12:20
  • Look like add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 10, 2); should be a add_filter to get this work,. – Mikhael Aubut Sep 30 '16 at 13:30
  • No, `add_action` and `add_filter` are the same functions. See [add_action source code](https://developer.wordpress.org/reference/functions/add_action/#source). But yes, it should be add_filter for better understanding. – Marek Oct 03 '16 at 06:51
  • Wonderful, still works - but how to use this if we do manage stock ? "manage stock" option enabled! – SolaceBeforeDawn Aug 07 '23 at 06:38
0

Well, I ended up hiding the former stock option dropdown in Javascript

add_action('woocommerce_product_options_stock_status', 'add_custom_stock_type');    
function add_custom_stock_type() {
        // Stock status - We remove the default one
        ?>
        <script type="text/javascript">
            jQuery('_stock_status').remove();
        </script>
        <?php   
    }

and created a new one using this tutorial: http://www.remicorson.com/mastering-woocommerce-products-custom-fields/ Not sure it's the cleanest solution but it doesn't touch the core files at least ! :)

Laila
  • 1,421
  • 3
  • 13
  • 27