Skip to content
Advertisement

How to directly update WooCommerce product stock quantity by its sku

A have a ton of variable products. I’m using cron to run the script every X minutes for fetching stock level from an external source. If I use wc_get_product_id_by_sku and update_post_meta it takes too long so I’m trying to find the way to use a custom prepared query. If I know product_id query would be like that:

JavaScript

But I only know a product SKU. So how to modify this query to update _stock based on a product SKU?

post_id meta_key meta_value
10 _sku ABCD
10 _stock 25

Advertisement

Answer

$wpdb and JOIN works too slow, so the fastest way is to use four separate prepared queries:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement