Set a Default Stock Quantity for WooCommerce Products February 17 2013

Here’s a quick little snippet that you can make use of if you want to set a default stock quantity for any newly added WooCommerce product Variations. This can be useful when bulk-adding variations and you don’t want an initial quantity of ”.
add_action( 'admin_enqueue_scripts', 'wc_default_variation_stock_quantity' );
function wc_default_variation_stock_quantity() {
global $pagenow, $woocommerce;
$default_stock_quantity = 0;
$screen = get_current_screen();
if ( ( $pagenow == 'post-new.php' || $pagenow == 'post.php' || $pagenow == 'edit.php' ) && $screen->post_type == 'product' ) {
ob_start();
?>
$('.woocommerce_variations').bind('woocommerce_variations_added',function() {
$('.woocommerce_variations input').each(function(index,el) {
el = $(el);
if(el.attr('name') && el.attr('name').substr(0,14) == 'variable_stock' && el.val() == '') {
el.val(<?php echo $default_stock_quantity; ?>);
}
});
});
<?php
$javascript = ob_get_clean();
$woocommerce->add_inline_js( $javascript );
}
}
Or, if you need a default stock quantity for simple products:
add_action( 'admin_enqueue_scripts', 'wc_default_variation_stock_quantity' );
function wc_default_variation_stock_quantity() {
global $pagenow, $woocommerce;
$default_stock_quantity = 0;
$screen = get_current_screen();
if ( ( $pagenow == 'post-new.php' || $pagenow == 'post.php' || $pagenow == 'edit.php' ) && $screen->post_type == 'product' ) {
ob_start();
?>
if ( '' === $( '#_stock' ).val() ) {
$( '#_stock' ).val(<?php echo $default_stock_quantity; ?>);
}
<?php
$javascript = ob_get_clean();
$woocommerce->add_inline_js( $javascript );
}
}
The post Set a Default Stock Quantity for WooCommerce Products appeared first on SkyVerge.