Created
March 12, 2021 21:40
-
-
Save seebeen/bc7083921d69d63843f1d9469546bbf1 to your computer and use it in GitHub Desktop.
[WooCommerce Product Manipulation] WooCommerce Product manipulation basics #wordpress #woocommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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