Title: WooCommerce product quantity change code
Last modified: April 4, 2020

---

# WooCommerce product quantity change code

 *  Resolved [domco](https://wordpress.org/support/users/domco/)
 * (@domco)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/woocommerce-product-quantity-change-code/)
 * Dear Community,
    I have a WooCommerce store (call it WooStore1) with product 
   stock. Because I have another online-store (call it OtherStore2) which is custom-
   built non-WP/WooCommerce, I have stock there too for the same products. Because
   the majority of the products have quantity of 1 and are gone once sold, I need
   to ensure that WooStore1 and OtherStore2 stock quantities are synchronised and
   maintain correct, so I don’t double sell. My initial idea is that in OtherStore2,
   when I have any stock quantity change system runs a function which updates stock
   quantity in WooStore2 via MySQL DB, by updating relevant _stock meta_value in
   DB. So, this is pretty simple to have OtherStore2 updating WooStore1 stock quantity.
   What I am stuck with, is if Product is sold in WooStore1, I don’t know how to
   update the OtherStore2. All products are Simple products, and no variations. 
   Is there a way to create a function or add a script to be run when stock quantity
   is changed in WooCommerce Product, or when order, sale, return, happens? Anyone
   could give some ideas, or propose a solution would be much appreciated. Many 
   thanks Dom

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

 *  [linux4me2](https://wordpress.org/support/users/linux4me2/)
 * (@linux4me2)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/woocommerce-product-quantity-change-code/#post-12628334)
 * There is a hook (action) called “woocommerce_order_status_changed” that it looks
   like you could use in your child theme’s functions.php to check the order status,
   get the product id(s) involved in the order, and then run some PHP to communicate
   to OtherStore2.
 *  [linux4me2](https://wordpress.org/support/users/linux4me2/)
 * (@linux4me2)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/woocommerce-product-quantity-change-code/#post-12628526)
 * Maybe something like:
 *     ```
       action('woocommerce_order_status_changed', 'custom_order_status_change', 10, 3);
       function custom_order_status_change($order_id, $from_status, $to_status) {
         $order = wc_get_order($order_id);
         foreach ($order->get_items() as $item_id => $item_data) {
           $product = $item_data->get_product();
           // Get the data you need to compare to OtherStore2
           $product_id = $product->get_id();
           $product_name = $product->get_name(); 
   
           $item_quantity = $item_data->get_quantity();
           // Do your communicating here, or create an array of product ids and quantities and do it after this loop using the $to_status to determine the math.
         }
       }
       ```
   
 *  [Remi Corson](https://wordpress.org/support/users/corsonr/)
 * (@corsonr)
 * Automattic Happiness Engineer
 * [6 years, 1 month ago](https://wordpress.org/support/topic/woocommerce-product-quantity-change-code/#post-12665942)
 * 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.

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

The topic ‘WooCommerce product quantity change code’ 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/)

## Tags

 * [MySQL](https://wordpress.org/support/topic-tag/mysql/)
 * [stock quantity](https://wordpress.org/support/topic-tag/stock-quantity/)
 * [synchronize](https://wordpress.org/support/topic-tag/synchronize/)

 * 3 replies
 * 3 participants
 * Last reply from: [Remi Corson](https://wordpress.org/support/users/corsonr/)
 * Last activity: [6 years, 1 month ago](https://wordpress.org/support/topic/woocommerce-product-quantity-change-code/#post-12665942)
 * Status: resolved