Title: Update stock programmatically
Last modified: July 4, 2020

---

# Update stock programmatically

 *  Resolved [Ruben](https://wordpress.org/support/users/rbnmo/)
 * (@rbnmo)
 * [6 years ago](https://wordpress.org/support/topic/update-stock-programmatically/)
 * Hello,
 * I’m trying to update variable product stocks through php with this function:
   
   wc_update_product_stock($post_id, $qty, ‘set’, false); But I sometimes end up
   with the stock status showing “Out of stock (3)” – so it has stock but it shows
   the wrong status.
 * I believe it doesn’t updated the stock status when it goes back in stock from
   0.
 * I also tried:
    wc_update_product_stock($post_id, $qty, ‘set’, true); but it still
   didn’t work.
 * How can I tell it to update the stock status? Is there a better way to quickly
   update stocks programmatically on multiple products through a cron job?
 * Thank you!
 * WooCommerce Version 4.0.1

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [Peter Lawrenson](https://wordpress.org/support/users/lorro/)
 * (@lorro)
 * [6 years ago](https://wordpress.org/support/topic/update-stock-programmatically/#post-13075028)
 * After changing product data programatically, ensure you have deleted the relevant
   transient:
    `wc_delete_product_transients( $post_id );` This will force the data
   to be re-evaluated the next time the product is queried.
 *  Thread Starter [Ruben](https://wordpress.org/support/users/rbnmo/)
 * (@rbnmo)
 * [6 years ago](https://wordpress.org/support/topic/update-stock-programmatically/#post-13075742)
 * Thank you very much, lorro!
    I will try that and get back to you if it doesn’t
   work.
 *  Plugin Support [Tseten a11n](https://wordpress.org/support/users/tibetanitech/)
 * (@tibetanitech)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/update-stock-programmatically/#post-13160444)
 * We haven’t heard back from you in a while, so I’m going to mark this as resolved–
   if you have any further questions, you can start a new thread.
 *  Thread Starter [Ruben](https://wordpress.org/support/users/rbnmo/)
 * (@rbnmo)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/update-stock-programmatically/#post-13193671)
 * Tried it and it still doesn’t work:
 *     ```
       $new_stock = wc_update_product_stock($post_id, $qty, 'set', false);
       wc_delete_product_transients($post_id);
       ```
   
 * I see this:
    Out of stock (8) Out of stock (3) etc..
 *  Thread Starter [Ruben](https://wordpress.org/support/users/rbnmo/)
 * (@rbnmo)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/update-stock-programmatically/#post-13193746)
 * I found out the issue. It was because the product is a variable product but has
   no variations.
 *  [NikkaG](https://wordpress.org/support/users/nikkag/)
 * (@nikkag)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/update-stock-programmatically/#post-13754554)
 * Hello,
 * I have the same issue. On Simple products my code works but on **Variations**
   the _stock_status is not updated.
    For example, if the stock quantity of a Variation
   is 30 and I change it to 0 with my code, the _stock_status remain “instock” instead
   of “outofstock”.
 * My code is:
 *     ```
       $product = wc_get_product( $variant_post_id );  
       wc_update_product_stock( $product,  $qty_new , 'set' );
       wc_delete_product_transients( $variant_post_id );
       ```
   
 * Thanks!
 * WooCommerce version 4.5.2
 *  [ramon fincken](https://wordpress.org/support/users/ramon-fincken/)
 * (@ramon-fincken)
 * [5 years, 6 months ago](https://wordpress.org/support/topic/update-stock-programmatically/#post-13843151)
 * Also see this nice CRUD example here: [https://stackoverflow.com/a/51940564/696680](https://stackoverflow.com/a/51940564/696680)

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Update stock programmatically’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 7 replies
 * 5 participants
 * Last reply from: [ramon fincken](https://wordpress.org/support/users/ramon-fincken/)
 * Last activity: [5 years, 6 months ago](https://wordpress.org/support/topic/update-stock-programmatically/#post-13843151)
 * Status: resolved