Add stock option in woocommerce

I would like to add a new option to the drop-down list of stock options for the product. By default, "Out of stock", "In stock", and I would like to add a third option.

I found a method that displays a 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 directly edit the Woocommerce class so that we can update Woocommerce without losing any custom code. Is there a way to override this method?

+7
coding-style php wordpress woocommerce
source share
2 answers

Well, I ended up hiding the stock options 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 if this is the cleanest solution, but at least it doesn't apply to core files! :)

0
source share

for all who are interested, here is a complete solution based on Leila's approach. A warning! My solution is designed to work only with the WoolCommerce inventory management option! I do not work with exact amounts in stock. All code goes into functions.php , as usual.

Inside

Deletes the field of the status drop-down list. Adding a CSS class to highlight my new custom field. A new option "On Demand" has appeared in the drop-down list.

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

Unfortunately, WooCommerce will only store instock or outofstock values using its own functions. Therefore, after processing the product data, I must again maintain the status of my stock.

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

Part of the template

get_availability() last thing: I need to change the data returned by get_availability() . When "inventory management" is disabled, WooCommerce again knows the values ​​of "instock" and "outofstock". Thus, I have a check on stock status myself.

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

Perhaps this is not a bulletproof solution ... In the end, I will update it.

+12
source share

All Articles