Woocommerce: Create product with variations in a single API call

Created on 24 Oct 2017  路  3Comments  路  Source: woocommerce/woocommerce

From the API documentation
https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-product

The example doesn't seem to show how to create a variable product together with its variations in one single call to products endpoint. I've already tried filling in the variations property in the request but it seems to be ignored.

Does this means to create a product variations we need to call /products//variations endpoint after we call products endpoint?

Most helpful comment

Thanks for the clarification, I've managed to create the variations using the above method.
It should be mentioned in the documentation though, I spent quite some time trying to achieve the same using only the 'products' endpoint.

The information I can find regarding the subject is this one
https://stackoverflow.com/questions/37823867/woocommerce-api-create-new-product-with-variations
which is kind of misleading seeing the data structure in the posted question.

I do hope that you consider adding the capability to the 'products' endpoint to create the variations in a single go, it makes more sense for me at least from ACID transaction point of view, it's possible that the second call to products//variations/batch failed and leaving the parent product with no child variations and in turns adding more maintenance work for the API client to clean it up in case of error.

All 3 comments

On https://woocommerce.github.io/woocommerce-rest-api-docs/?php#create-a-product see the example of how to create a variable product. From there you will unfortunately have to create variations separate as variations are stores as products each in the database, that is done via the /products//variations endpoint which you then link to the parent product created first.

Does this means to create a product variations we need to call /products//variations endpoint after we call products endpoint?

Yes, but you can create all variations at the same time: https://woocommerce.github.io/woocommerce-rest-api-docs/?php#batch-update-product-variations

Thanks for the clarification, I've managed to create the variations using the above method.
It should be mentioned in the documentation though, I spent quite some time trying to achieve the same using only the 'products' endpoint.

The information I can find regarding the subject is this one
https://stackoverflow.com/questions/37823867/woocommerce-api-create-new-product-with-variations
which is kind of misleading seeing the data structure in the posted question.

I do hope that you consider adding the capability to the 'products' endpoint to create the variations in a single go, it makes more sense for me at least from ACID transaction point of view, it's possible that the second call to products//variations/batch failed and leaving the parent product with no child variations and in turns adding more maintenance work for the API client to clean it up in case of error.

Was this page helpful?
0 / 5 - 0 ratings