• Resolved domco

    (@domco)


    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)
  • 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.

    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

    (@corsonr)

    Automattic Happiness Engineer

    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.