1

I need to create a product manually from php script (e.g. i'm developing a custom front-end form to insert products)

Googling i see some support forum create manually a post (using wp_insert_post function). I think this solution is not reliable (with a woocommerce update things may changes).

I need a wc function that does it for me, a sort of wc_create_product (like wc_create_order function).

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. Additionally this function is not listed on woocommerce official api doc at 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

luk_z
  • 451
  • 1
  • 8
  • 16
  • It is in the [REST API docs](http://woothemes.github.io/woocommerce-rest-api-docs/#create-a-product) – helgatheviking Oct 30 '15 at 17:00
  • Thanks, you are right! So i guess the function it is only usable within REST protocol... So my question persist: is there a function to create product in a font-end form?? i don't know why all post type have their crete function except wc-product.. – luk_z Nov 02 '15 at 09:35
  • No other post types have specific creation functions. Any/all post types can be created via [`wp_insert_post()`](https://codex.wordpress.org/Function_Reference/wp_insert_post) – helgatheviking Nov 02 '15 at 13:39

1 Answers1

4

In Woocommerce, products are represented by objects of the class WC_Product which is documented here.

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')
...
Philipp Zedler
  • 1,660
  • 1
  • 17
  • 36