I would like to programmatically create a variable product ("parent" product) with two new variante attributes - all that from a Wordpress plugin (so no HTTP Request to an API).
These two variante attributes should also be created on the fly.
How can this be done ?
(with Woocommerce version 3)
Update : I have written more lines of codes on this that I wished, and tried many things to solve it, using woocommerce objects, and added missing data about terms, termmeta, the relationship from term with post, in the database using the wordpress database object - but nothing has sufficed to make it work. And I couldn't pin-point where I went wrong - that is why I couldn't provide a narrower problem - things for which stackoverflow is more made for.
Here you get the way to create a new variable product with new product attributes + values:
Code goes in function.php file of your active child theme (or active theme). Tested and works.
USAGE (example with 2 new attributes + values):
Tested and works.
Related:
You can also achieve this using the new native functions for setting/getting data from postmeta.
Here is an example that works ( based on the default dummy products of Woocommerce 3)
You can add things like weight, tax, sku etc using the native functions available from Woocommerce 3 and on like set_price, set_sku ect.
Wrap it within a function and you are good to go.