Title: Variable Product Stock Quantity &#8211; SQL Query
Last modified: August 31, 2016

---

# Variable Product Stock Quantity – SQL Query

 *  [brafal](https://wordpress.org/support/users/brafal/)
 * (@brafal)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/variable-product-stock-quantity-sql-query/)
 * I am trying to find a query that will return the stock quantity of a variable
   product, by searching it using the SKU of the variation.
 * Also I need to update the Stock of the product by SKU through the databse.
 * Any ideas and how to resolve this?
 * Thanks, much appriciated.
 * [https://wordpress.org/plugins/woocommerce/](https://wordpress.org/plugins/woocommerce/)

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

 *  Plugin Contributor [Mike Jolley](https://wordpress.org/support/users/mikejolley/)
 * (@mikejolley)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/variable-product-stock-quantity-sql-query/#post-7293983)
 * You can get the ID via a WooCommerce function – wc_get_product_id_by_sku
 * [https://docs.woothemes.com/wc-apidocs/function-wc_get_product_id_by_sku.html](https://docs.woothemes.com/wc-apidocs/function-wc_get_product_id_by_sku.html)
 * Then grab it using `wc_get_product()` and finally call `$product->set_stock( 
   X )` to set the stock value.
 * Doing it via SQL would require querying the postmeta table instead.
 *  Thread Starter [brafal](https://wordpress.org/support/users/brafal/)
 * (@brafal)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/variable-product-stock-quantity-sql-query/#post-7294015)
 * Thank you for the explanation, but I have to do it using SQL query, since that
   is the requirement at the moment. I am giving access to third-party service to
   change the quantity of a product (finding it by SKU) by accessing the database.
 * I’ve been looking for queries online for weeks but cant find anything. And since
   my SQL knowledge is limited, I am not able to write the whole query myself.
 *  Thread Starter [brafal](https://wordpress.org/support/users/brafal/)
 * (@brafal)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/variable-product-stock-quantity-sql-query/#post-7294029)
 * Or if there is alternative PHP code I could use to trigger quantity change by
   specific SKU and Stock Quantity.
 *  Plugin Contributor [Mike Jolley](https://wordpress.org/support/users/mikejolley/)
 * (@mikejolley)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/variable-product-stock-quantity-sql-query/#post-7294030)
 * ^ what I showed you earlier was PHP. You’re going to be using the WordPress environment
   anyway so probably not worth using direct SQL.
 *  [aboisclair](https://wordpress.org/support/users/aboisclair/)
 * (@aboisclair)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/variable-product-stock-quantity-sql-query/#post-7294302)
 * I also have been needing to do this with SQL.
 * Currently I am returning a bunch of jason-type data.
    i.e the following example
   from the SQL row/colum quantity by size: a:5:{s:2:”XS”;s:1:”0″;s:1:”S”;s:1:”1″;
   s:1:”M”;s:1:”1″;s:1:”L”;s:1:”0″;s:2:”XL”;s:1:”0″;}
 * Not sure how to decipher this either. I am writing a plugin to export order and
   product data. I am down to my final query, which happens to require getting quantity
   per size per item.
 * Any thoughts?
 *  Plugin Contributor [Mike Jolley](https://wordpress.org/support/users/mikejolley/)
 * (@mikejolley)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/variable-product-stock-quantity-sql-query/#post-7294303)
 * This is serialised data.

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

The topic ‘Variable Product Stock Quantity – SQL Query’ 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

 * [database](https://wordpress.org/support/topic-tag/database/)
 * [MySQL](https://wordpress.org/support/topic-tag/mysql/)
 * [variable product](https://wordpress.org/support/topic-tag/variable-product/)

 * 6 replies
 * 3 participants
 * Last reply from: [Mike Jolley](https://wordpress.org/support/users/mikejolley/)
 * Last activity: [9 years, 11 months ago](https://wordpress.org/support/topic/variable-product-stock-quantity-sql-query/#post-7294303)
 * Status: not resolved