WordPress Stock Update Programatically

WordPress stores three levels of stock information on the wp_postmeta.

  1. Each product has manage stock enabled
  2. Stock Quantity
  3. Stock Status

Since the products on the above questions were imported, few of them didn’t had manage stock enabled so updating the stock quantity to 0 didn’t work well.

Solution:
I forcefully updated the manage stock to be yes and then trashed the quantity to be 0 with status out of stock. Further to achieve the second part, where the select statement pulls the data based on the last modified, I forcefully pushed the product to draft state. This would modify the datetime and would not be involved on the next polling.

update_post_meta($product_id, '_manage_stock','Yes');
update_post_meta($product_id, '_stock', '0');
update_post_meta($product_id, '_stock_status', 'outofstock');

wp_update_post(
        array( 
            'ID' => $product_id, 
            'post_status' => 'draft'
        )
    );