Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crisweiser/a322f1bf0e6edc183c9e to your computer and use it in GitHub Desktop.
Save crisweiser/a322f1bf0e6edc183c9e to your computer and use it in GitHub Desktop.
Change Woocommerce Stock Status
<?php
/**
* set the stock status for all products in your WooCommerce store
* $from is the searched value
* $signal is the operator (< or > ..)
* $to is the target to be defined
* @return void
*/
function woocommerce_update_stock_status($from,$signal,$to){
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 $signal $from ) id SET stock.meta_value = '$to' 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