Changing and creating products
A product is changed through the same objects it is read with: every get_... method has a set_... pair. Changes stay in memory until save() is called — it writes the product to the database and updates everything related.
Where to run it
One-off operations — creating products, raising prices — are convenient to run from the console. Create a scripts folder in the plugin and the file scripts/update-mug.php:
// wp-content/plugins/acme-shop/scripts/update-mug.php
$product = wc_get_product(12);
$product->set_regular_price('11.90');
$product->save();
echo 'New price: ' . $product->get_regular_price() . PHP_EOL;The file starts with <?php. Run it from the site folder:
wp eval-file wp-content/plugins/acme-shop/scripts/update-mug.phpThe command loads WordPress, runs the file and prints New price: 11.90. Refresh the product page — the price has changed.
Setter methods
| Method | What it sets |
|---|---|
set_name('Mug') | The name |
set_regular_price('11.90') | The regular price |
set_sale_price('9.90') | The sale price, an empty string removes the sale |
set_sku('MUG-001') | The SKU |
set_manage_stock(true) | Turns stock management on |
set_stock_quantity(15) | The stock |
set_status('draft') | The status: publish, draft, private |
set_category_ids([15]) | The categories |
Prices are passed as strings with a dot.
Creating a product
A new product is a new object of the right class. The file scripts/create-plate.php:
// wp-content/plugins/acme-shop/scripts/create-plate.php
$product = new WC_Product_Simple();
$product->set_name('Plate');
$product->set_sku('PLATE-001');
$product->set_regular_price('7.50');
$product->set_short_description('Ceramic plate, 22 cm.');
$product->set_manage_stock(true);
$product->set_stock_quantity(40);
$product->set_status('publish');
$id = $product->save();
echo "Created product $id" . PHP_EOL;save() returns the ID of the new product. After running it with wp eval-file the plate appears in Products and on the /shop/ page.
Your own data
For properties a product does not have there is meta data — key and value pairs attached to the product:
// wp-content/plugins/acme-shop/scripts/update-mug.php
$product = wc_get_product(12);
$product->update_meta_data('_acme_lead_time', 3);
$product->save();
echo $product->get_meta('_acme_lead_time') . PHP_EOL;The script stores a lead time of three days and prints 3. The key starts with an underscore so that it is hidden from the Custom Fields box of the editor, and with the acme prefix so that it does not clash with someone else's.
Changing on an event
The same methods work inside hooks. For example, hiding a product when it runs out of stock:
// wp-content/plugins/acme-shop/includes/products.php
add_action('woocommerce_no_stock', function (WC_Product $product) {
$product->set_catalog_visibility('hidden');
$product->save();
});The woocommerce_no_stock hook fires when a product's stock reaches zero. The product disappears from the catalog and search but stays reachable by a direct link.
Deleting
$product->delete(); // to the trash
$product->delete(true); // permanentlyCommon mistake
The price is changed directly: update_post_meta($id, '_price', 10). The database row changes, but the product cache and the wp_wc_product_meta_lookup lookup table stay old — in the catalog, sorting and filters the product keeps the old price. Change a product only with set_... methods and save().