Skip to content

Instantly share code, notes, and snippets.

@vmanthos
Created February 18, 2020 07:17
Show Gist options
  • Save vmanthos/8f22925ae2fbcebbd48aa5abf453ff74 to your computer and use it in GitHub Desktop.
Save vmanthos/8f22925ae2fbcebbd48aa5abf453ff74 to your computer and use it in GitHub Desktop.
<?php
require( 'wp-load.php' );
define('WP_USE_THEMES', false);
$sku = 'wp-pennant';
// get the product ID from the SKU
$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
// Get an instance of the WC_Product object
$product = new WC_Product( $product_id );
// Get product stock quantity and stock status
$stock_quantity = $product->get_stock_quantity();
$stock_status = $product->get_stock_status();
// Display stock quantity and status
echo '<p>Product Stock quantity: ' . $stock_quantity;
// Set product stock quantity
$product->set_stock_quantity( 3 );
// Save the data/clear caches
$product->save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment