简体   繁体   English

woocommerce功能以编程方式创建产品

[英]woocommerce function to create product programmatically

I need to create a product manually from php script (eg i'm developing a custom front-end form to insert products) 我需要通过php脚本手动创建产品(例如,我正在开发自定义的前端表单以插入产品)

Googling i see some support forum create manually a post (using wp_insert_post function). 谷歌搜索我看到一些支持论坛手动创建一个帖子(使用wp_insert_post函数)。 I think this solution is not reliable (with a woocommerce update things may changes). 我认为此解决方案不可靠(随着woocommerce更新,情况可能会发生变化)。

I need a wc function that does it for me, a sort of wc_create_product (like wc_create_order function). 我需要一个为我做的wc函数,一种wc_create_product(例如wc_create_order函数)。

I've found WC_API_Products::create_product() seems to be what i need but on the web i could not find any additional documentation or usage example except the code comment. 我发现WC_API_Products::create_product()似乎是我所需要的,但是在Web上,除了代码注释之外,我找不到任何其他文档或用法示例。 Additionally this function is not listed on woocommerce official api doc at https://docs.woothemes.com/wc-apidocs/ . 此外,此功能未在woocommerce官方api文档上列出,网址为https://docs.woothemes.com/wc-apidocs/

I need to know if this function is thought for internal use or maybe for external developer like me. 我需要知道此功能是供内部使用还是像我这样的外部开发人员使用。

Does anyone have some information about it? 有人知道吗?

thanks 谢谢

In Woocommerce, products are represented by objects of the class WC_Product which is documented here . 在Woocommerce,产品是由类的对象表示WC_Product其被记录在这里

The documentation dicourages us from using the constructor explicitly, however, this works. 文档使我们不愿明确使用构造函数,但是,这样做是可行的。 We can create products by calling the constructor and then add properties. 我们可以通过调用构造函数来创建产品,然后添加属性。

$product = new WC_Product();
$product->set_name('My Product')
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM