<?php
include( 'wp-load.php' );
// Setup your custom query
$args = [
'post_type' => ['product'/*, 'post', 'page'*/],
'posts_per_page' => -1,
'post_status' => 'publish'
];
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $post;
$product = wc_get_product( $post->ID );
if($product->get_stock_quantity()>0 && $product->get_manage_stock() == 'yes' && $post->post_name != 'bra'){
echo 'Update stock for simple product ID: '.$post->ID.' slug: '.$post->post_name.' Title: '.$product->get_title().' Old quantity: '.$product->get_stock_quantity().'<br>';
// Using WC setters
$product->set_manage_stock(false);
$product->set_stock_quantity(0);
$product->set_stock_status('instock');
// Save Product
$product->save();
}
if ($product->get_type() == "variable" && $post->post_name != 'bra') {
$variations = $product->get_available_variations();
foreach($variations as $variation){
$variation_id = $variation['variation_id'];
$variation_obj = new WC_Product_variation($variation_id);
if($variation_obj->get_stock_quantity()>0 && $variation_obj->get_manage_stock() == 'yes'){
echo 'Update stock for variation product ID: '.$variation_id .' slug: '.$post->post_name.' Title: '.$variation_obj->get_title().' Old quantity: '.$variation_obj->get_stock_quantity().'<br>';
// Using WC setters
$variation_obj->set_manage_stock(false);
$variation_obj->set_stock_quantity(0);
$variation_obj->set_stock_status('instock');
// Save Product
$variation_obj->save();
}
}
}
endwhile; wp_reset_query(); // Remember to reset
echo 'stock update done';
?>