Skip to content

Instantly share code, notes, and snippets.

@Tomasz-Silpion
Created March 10, 2017 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tomasz-Silpion/fd3727b95b22409dc505368099f7d1fd to your computer and use it in GitHub Desktop.
Save Tomasz-Silpion/fd3727b95b22409dc505368099f7d1fd to your computer and use it in GitHub Desktop.
Fix Woocommerce stock to match qty
<?php
define('WP_USE_THEMES', false);
require('./wp-load.php');
$args = array('post_type' => 'product', 'posts_per_page' => -1);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
$qty = get_post_meta(get_the_ID(), '_stock', true);
$status = get_post_meta(get_the_ID(), '_stock_status', true);
if ($qty > 0 && $status == "outofstock") {
update_post_meta(get_the_ID(), '_stock_status', 'in_stock', $status);
echo sprintf("%s qty was %s, status was %s. Fixed stock to instock", get_the_title(), $qty, $status);
echo "<br>";
}
endwhile;
wp_reset_query();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment