Skip to content

Instantly share code, notes, and snippets.

@stuart-lambon
Created September 9, 2018 19:45
Show Gist options
  • Save stuart-lambon/52f256671fe527be6bb9a25ae7cda355 to your computer and use it in GitHub Desktop.
Save stuart-lambon/52f256671fe527be6bb9a25ae7cda355 to your computer and use it in GitHub Desktop.
Updates WooCommerce product stock quantity programmatically. $sku and $qty variables need to be filled.
<?php
$sku = 'ITEM001';
$qty = 1;
// Get product_id from SKU — returns null if not found
$product_id = wc_get_product_id_by_sku( $sku );
// Process if product found
if ( $product_id != null ) {
// Check for negative stock (backorders etc.) and set to 0
if ( $qty <= 0 ) {
$qty = 0;
}
// Set up WooCommerce product object
$product = wc_get_product( $product_id );
// Make changes to stock quantity and save
$product->set_manage_stock( true );
$product->set_stock_quantity( $qty );
$product->save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment