The stock statuses from my woocommerce installation were uploaded by another plugin (square) and so all goods are in stock even if they have quantity 0. Is there a way to get the statuses to reset somehow, so that products with 0 stock, have the stock status "out of stock".
I have looked for this and found the following function
function jolie_theme_child_reset_stock_status(){
global $wpdb;
// set all status for products with 0 or less stocked quantity
$sql = "UPDATE $wpdb->postmeta stock, (SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = '_stock' AND meta_value < 1 ) id SET stock.meta_value = 'outofstock' WHERE stock.post_id = id.post_id AND stock.meta_key = '_stock_status';";
// set all status for products with stock.
$sql .= "UPDATE $wpdb->postmeta stock, (SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = '_stock' AND meta_value > 0 ) id SET stock.meta_value = 'outofstock' WHERE stock.post_id = id.post_id AND stock.meta_key = '_stock_status';";
// run queries
$wpdb->query( $sql );
}
But if I run this it causes a server 500 error. What is a simple way to reset the stock statuses with or without using the above code?
The stock statuses from my woocommerce installation were uploaded by another plugin (square) and so all goods are in stock even if they have quantity 0. Is there a way to get the statuses to reset somehow, so that products with 0 stock, have the stock status "out of stock".
I have looked for this and found the following function
function jolie_theme_child_reset_stock_status(){
global $wpdb;
// set all status for products with 0 or less stocked quantity
$sql = "UPDATE $wpdb->postmeta stock, (SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = '_stock' AND meta_value < 1 ) id SET stock.meta_value = 'outofstock' WHERE stock.post_id = id.post_id AND stock.meta_key = '_stock_status';";
// set all status for products with stock.
$sql .= "UPDATE $wpdb->postmeta stock, (SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = '_stock' AND meta_value > 0 ) id SET stock.meta_value = 'outofstock' WHERE stock.post_id = id.post_id AND stock.meta_key = '_stock_status';";
// run queries
$wpdb->query( $sql );
}
But if I run this it causes a server 500 error. What is a simple way to reset the stock statuses with or without using the above code?
Share Improve this question asked Sep 6, 2017 at 9:24 NYoungNYoung 111 silver badge2 bronze badges2 Answers
Reset to default 1You can add the following sql command in your database using PHP my admin or anyother DB editor
Update wp_postmeta Set meta_value = 'instock' Where meta_value = 'outofstock' And meta_key = '_stock_status'
Use below query for update Woo-commerce product status change manually.
Query:-
Update wp_postmeta Set meta_value = 'instock' Where meta_value = 'outofstock' And meta_key = '_stock_status'
Note:- First check your table name before run this query. If your table name prefix not default then change it.