This code sets your single product > inventory > stock management setting to allow by default. On your inventory tab, it does 3 things :
- Sets the Stock Management checkbox to yes enabling you to track stock quantity for this product.
- Sets the Allow Backorders radio button to “Allow” by default on all single product pages.
- Enables the default settings to be changed per product ( use 2nd snippet ).
Here’s the result, click to enlarge :
![](https://wpsites.net/wp-content/uploads/2024/07/stock-management-allow-backorders-default-600x244.png)
Here’s a demo video showing how the 1st snippet works :
There’s 2 code snippets in this tutorial :
- The 1st snippet sets the default so it cannot be changed manually on any single product
- The 2nd snippet sets the default and allows manually changing of the settings on a per product page basis.
Add the following code to the end of your child themes functions.php file or custom code snippets plugin.
add_action('woocommerce_product_options_inventory_product_data', 'set_default_stock_management_and_backorder_setting_06');
add_action('woocommerce_process_product_meta', 'save_default_stock_management_and_backorder_setting_06');
function set_default_stock_management_and_backorder_setting_06() {
global $post;
// Check if we are editing an existing product
if ($post && get_post_type($post->ID) === 'product') {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
// Check the "Manage stock?" checkbox
var manageStockCheckbox = $('#_manage_stock');
if (!manageStockCheckbox.is(':checked')) {
manageStockCheckbox.prop('checked', true).trigger('change');
}
// Set default value for backorders to "Allow"
var backordersAllow = $('input[name="_backorders"][value="yes"]');
var backordersNotify = $('input[name="_backorders"][value="notify"]');
var backordersNo = $('input[name="_backorders"][value="no"]');
if (backordersNo.is(':checked') || backordersNotify.is(':checked')) {
backordersAllow.prop('checked', true);
}
});
</script>
<?php
}
}
function save_default_stock_management_and_backorder_setting_06($post_id) {
$product = wc_get_product($post_id);
// Enable stock management
if (!$product->get_manage_stock()) {
update_post_meta($post_id, '_manage_stock', 'yes');
}
// Set default backorder setting to "Allow" if it's not already set
if ($product->get_backorders() !== 'yes') {
update_post_meta($post_id, '_backorders', 'yes');
}
}
However, you might want to change the settings manually on some products. In this case, use the following code snippet instead.
add_action('admin_init', 'set_default_stock_management_and_backorder_for_existing_products_once');
function set_default_stock_management_and_backorder_for_existing_products_once() {
// Check if we have already set defaults
$defaults_set = get_option('defaults_set_for_existing_products');
if (!$defaults_set) {
// Get all products
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
);
$products = new WP_Query($args);
if ($products->have_posts()) {
while ($products->have_posts()) {
$products->the_post();
$product_id = get_the_ID();
$product = wc_get_product($product_id);
// Enable stock management if not already enabled
if ($product && !$product->get_manage_stock()) {
update_post_meta($product_id, '_manage_stock', 'yes');
}
// Set default backorder setting to "Allow" if not already set
if ($product && $product->get_backorders() !== 'yes') {
update_post_meta($product_id, '_backorders', 'yes');
}
}
wp_reset_postdata();
// Mark defaults as set
update_option('defaults_set_for_existing_products', true);
}
}
}
The code can be customized to do anything you like!
Need More Coding Help?
Was This Tutorial Helpful?