Created
July 7, 2013 14:11
-
-
Save codearachnid/5943589 to your computer and use it in GitHub Desktop.
set the stock status for all products in your WooCommerce store
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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