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 ?
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”.
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.
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.Maybe it’s not bulletproof solution … I will update it, eventually.
Well, I ended up hiding the former stock option dropdown in Javascript
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 ! 🙂