TimonAndries
TimonAndries

Reputation: 33

SQL query to change backorder status of all products in WooCommerce

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?

Upvotes: 2

Views: 4248

Answers (4)

MH Shakib
MH Shakib

Reputation: 1

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

Upvotes: 0

MH Shakib
MH Shakib

Reputation: 1

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

Upvotes: -1

Saad Mateen
Saad Mateen

Reputation: 21

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

Upvotes: 2

mujuonly
mujuonly

Reputation: 11861

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

Upvotes: 5

Related Questions