Skip to content

Instantly share code, notes, and snippets.

@seebeen
Created March 12, 2021 21:40
Show Gist options
  • Save seebeen/bc7083921d69d63843f1d9469546bbf1 to your computer and use it in GitHub Desktop.
Save seebeen/bc7083921d69d63843f1d9469546bbf1 to your computer and use it in GitHub Desktop.
[WooCommerce Product Manipulation] WooCommerce Product manipulation basics #wordpress #woocommerce
<?php
$product_id = 202;
$product_sku = 'SIFRA-1245';
/*
This won't work because update_post_meta does not trigger necessary additions to product lookup tables and transients
*/
update_post_meta($product_id, '_sku', $product_sku);
$check_id = wc_get_product_id_by_sku($product_sku);
var_dump($check_id); // Returns 0
/*
Real deal
*/
$product = wc_get_product($product_id);
$product->set_sku($product_sku);
$product->save();
$check_id = wc_get_product_id_by_sku($product_sku);
var_dump($check_id); // Returns 202
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment