I wrote the following code to create a simple woocommerce product programmatically, but it always creates two products with different SKUs (i.e. when I comment out the line where the SKU is being set). If I allow the SKU to be set, then it throws an error about a duplicate SKU. I am not sure why two products are being created, it's really weird. Any idea how to fix this please?
$title = "This is a test title";
$catID = 100;
$my_sku = "425346FBD9CD4F5044384E6B52AC3747"; // This is a random string each time
$memo = "Lorem ipsum dolor sit amet consecteteur adipiscing elit"; // Some text
$prod = new WC_Product_simple();
$prod->set_name($title);
$prod->set_category_ids(array($catID)); // Set the product category
$prod->set_price(0); // set product price
//$prod->set_sku($my_sku);
$prod->set_stock_status('instock');
$prod->set_catalog_visibility('visible');
$prod->set_sold_individually(false);
$prod->set_status("publish");
$productID = $prod->save(); // Save up, and now we have a productID
// I need the productID so that I can do the following post meta update
// Save memo meta for the product
update_post_meta($productID, 'product_memo', $memo);
Edit: Woocommerce 4.8 and Wordpress 5.6, PHP 7.4
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…