Last active
May 14, 2020 10:12
-
-
Save shohag-biswas/031ab3f81e8372c2f809d6f2f4fdb1d1 to your computer and use it in GitHub Desktop.
Woocommerce Extra stock option
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*adding stock option*/ | |
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' => __( 'Available to Order', '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->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' => __( 'Available to Order', 'woocommerce' ), 'class' => 'on-request' ); | |
break; | |
} | |
return $data; | |
} | |
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 10, 2); |
Thanks for the code.
WC 4.1 : I tried it and it seems not able to work properly. As of WC 4.1, there's 3 default status, anything we added beyond that three, it only shows in the backend admin product pages. In WC frontend will end up as Instock.
For example : Added Preorder & Contact Us, it became Instock in the frontend.
Need to solve this soon!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great work... ..
This works only for simple products... what should I change / add for variation products
thanks