Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save gaelgerard/1eee5e7dd47ad1e8283917b2a1a5556f to your computer and use it in GitHub Desktop.

Select an option

Save gaelgerard/1eee5e7dd47ad1e8283917b2a1a5556f to your computer and use it in GitHub Desktop.

Revisions

  1. @stuart-lambon stuart-lambon created this gist Sep 9, 2018.
    25 changes: 25 additions & 0 deletions woo-update-product-stock-qty.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    <?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();

    }