title: Function that retrieves the stock quantity from a product in WooCommerce
body: <p>I'm trying to create a function that retrieves the stock quantity from a product in WooCommerce.
  If I comment out the following line the code runs but if I leave it in I get a white screen:
      $product = is_a( $product, 'WC_Product' ) ? $product : wc_get_product($product_id);</p>
  
  <p>Any ideas what I'm doing wrong here?</p>
  
  <pre><code>function wc_get_variable_product_stock_quantity( $output = 'raw', $product_id = 0 ){
      global $wpdb, $product;
  
      // Get the product ID (can be defined)
      $product_id = $product_id &gt; 0 ? $product_id : get_the_id();
  
      // Check and get the instance of the WC_Product Object
      $product = is_a( $product, 'WC_Product' ) ? $product : wc_get_product($product_id);
      $stock_quantity = $product-&gt;get_stock_quantity();
  
      return $stock_quantity;
  
  }
  </code></pre>
  
  <hr>
  
  <p>EDIT (UPDATE):</p>
  
  <p>My end goal is to sum the stock quantities for all variations after order status changes to complete and then write the total to the parent product's stock quantity so the total stock quantity is always up to date.</p>
  
  <p>My over simplified example above should actually look like the below code mostly based on "<a href="https://stackoverflow.com/questions/52857156/get-the-total-stock-of-all-variations-from-a-variable-product-in-woocommerce/52861024#52861024">Get the total stock of all variations from a variable product In Woocommerce</a>" answer code, with light changes <em>(see the additional SQL statement)</em>:</p>
  
  <pre><code>    function wc_get_variable_product_stock_quantity( $output = 'raw', $product_id = 0 ){
          global $wpdb, $product;
  
          // Get the product ID (can be defined)
          $product_id = $product_id &gt; 0 ? $product_id : get_the_id();
  
          // Check and get the instance of the WC_Product Object
          $product = is_a( $product, 'WC_Product' ) ? $product : wc_get_product($product_id);
          // Get the stock quantity sum of all product variations (children)
          $stock_quantity = $wpdb-&gt;get_var("
              SELECT SUM(pm.meta_value)
              FROM {$wpdb-&gt;prefix}posts as p
              JOIN {$wpdb-&gt;prefix}postmeta as pm ON p.ID = pm.post_id
              WHERE p.post_type = 'product_variation'
              AND p.post_status = 'publish'
              AND p.post_parent = '$product_id'
              AND pm.meta_key = '_stock'
              AND pm.meta_value IS NOT NULL
          ");   
          return $stock_quantity;
      }
  
  
  // Update stock totals for parent product when order status changes to completed...
  function mysite_woocommerce_order_status_completed( $order_id ) {
  
      // For each item in the order, calculate the total stock quantity for all variations and write the total to the parent products's stock quantity
      $order = wc_get_order( $order_id );
      $order_id = $order-&gt;get_id();
  
      $log_txt = '';
      $items = $order-&gt;get_items();
      foreach($items as $k=&gt;$val){     
          $product_id = $val[‘product_id’];
          $total_stock_quantity = wc_get_variable_product_stock_quantity( 'raw', $product_id );
          $log_txt .= 'Product ID:'.$val['product_id'].', Name: '.$val['name'].', Total Stock: '.$total_stock_quantity.'\n&lt;br&gt;';
          // Next: add code to write the stock total to the parent product
      }
  }
  
  add_action( 'woocommerce_order_status_completed', 'mysite_woocommerce_order_status_completed', 10, 1 );
  </code></pre>