Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save codearachnid/5943589 to your computer and use it in GitHub Desktop.
Save codearachnid/5943589 to your computer and use it in GitHub Desktop.
set the stock status for all products in your WooCommerce store
<?php
/**
* set the stock status for all products in your WooCommerce store
* @return void
*/
function woocommerce_update_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 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment