0
votes

In WooCommerce you can change the status of a product to allow backorders, the default setting is 'not allowed'. I have thousands of products on my shop and I rather not change it manually for all of them. Is there an SQL query that could do this for me directly in the database?

4

4 Answers

3
votes

Try running this query in mySql

 UPDATE wp_postmeta SET  meta_value = 'yes' WHERE meta_key = '_backorders';

This is not enough to achieve your purpose. Need to enable Manage stock as well.

Try running this query also if WooCommerce allow backorders global settings is not enabled.

UPDATE wp_postmeta SET  meta_value = 'yes' WHERE meta_key = '_manage_stock';

Need to update stock quantity as well

UPDATE wp_postmeta SET  meta_value = 1000 WHERE meta_key = '_stock';

For stock status

UPDATE wp_postmeta SET  meta_value = 'instock' WHERE meta_key = '_stock_status';
1
votes

I am working with variable products, so I need to execute the below query as well:

UPDATE wp_postmeta SET meta_value = 'onbackorder' WHERE meta_key = '_stock_status';
0
votes

If u want to auto then Edit Plugin Code.

I try, and it's work.

Woocommerce Stock Edit File.. /wp-content/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-product-data.php

Update It :

    'manage_stock'       => "yes",
    'backorders'         => "notify",
    'stock_status'       => "onbackorder",

Like Image

0
votes

it's work.

$product = SELECT * FROM `wpuk_posts` WHERE `post_type` = 'product';
foreach( $product as $pro){       
  $checknotify = SELECT * FROM `wpuk_postmeta` WHERE `post_id` = '$pro->ID' AND `meta_key` = '_stock_status';
        
  if (empty($checknotify)) {
     INSERT INTO `wpuk_postmeta`(`post_id`, `meta_key`, `meta_value`) VALUES ('$pro->ID','_stock_status','onbackorder');
  } else {
     UPDATE `wp_postmeta` SET meta_value = 'onbackorder' WHERE `post_id` = '$pro->ID' AND `meta_key` = '_stock_status';
  }
 }

UPDATE `wp_postmeta` SET meta_value = 'yes' WHERE `meta_key` = '_manage_stock';

It is for Notification

$product = SELECT * FROM `wpuk_posts` WHERE `post_type` = 'product'; // Optional
foreach( $product as $pro){   
$checknotify = SELECT * FROM `wpuk_postmeta` WHERE `post_id` = '$pro->ID' AND `meta_key` = '_backorders';

if (empty($checknotify)) {
    INSERT INTO `wpuk_postmeta`(`post_id`, `meta_key`, `meta_value`) VALUES ('$pro->ID','_backorders','notify');
} else {
    UPDATE `wp_postmeta` SET `meta_value` = '_backorders' WHERE `post_id` = '$pro->ID' AND `meta_key` = 'notify';
}
}