Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

php - Woocommerce creating duplicate simple products in programmatic function

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...