WooCommerce REST API – Create Products with Attributes
There are two types of attributes in WooCommerce – custom attributes and predefined ones. Custom attributes can be created individually on the product pages, to create a predefined attribute, go to Products — Attributes in admin menu.
Create a Product with Custom Product Attributes
Read more here how to get a pair of consumer key and secret.
$api_response = wp_remote_post( 'https://rudrastyh.com/wp-json/wc/v2/products', array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'KEY:SECRET' )
),
'body' => array(
'name' => 'My test2',
'regular_price' => '21.99',
'attributes' => array(
array(
'name' => 'Color', // parameter for custom attributes
'visible' => true, // default: false
'options' => array(
'black',
'blue'
)
)
)
)
) );
$body = json_decode( $api_response['body'] );
//print_r( $body );
if( wp_remote_retrieve_response_message( $api_response ) === 'Created' ) {
echo 'The product ' . $body->name . ' has been created successfully';
}
If you go to the edit product page, you will see something like this on the Attributes tab:
Add Predefined Attributes to already Existing Products
If you want to use already existing attribute, you have to specify its ID which can be found on the Products — Attributes page, just find the attribute you need, click the Edit button and look at the URL.
$api_response = wp_remote_post( 'https://rudrastyh.com/wp-json/wc/v2/products/PRODUCT ID', array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'KEY:SECRET' )
),
'body' => array(
'attributes' => array(
array(
'name' => 'Color',
'position' => 1,
'visible' => true, // default: false
'options' => array(
'black',
'blue'
)
),
array(
'id' => '2',
'position' => 0,
'variation' => true, // for variative products in case you would like to use it for variations
'options' => array(
'Big',
'SuperBig' // if the attribute term doesn't exist, it will be created
)
)
)
)
) );
$body = json_decode( $api_response['body'] );
//print_r( $body );
if( wp_remote_retrieve_response_message( $api_response ) === 'OK' ) {
echo 'The product ' . $body->name . ' has been updated';
}

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Hello thanks for the post
i dont understand where to add this code and is it possible to create the product using javascript?
i.e a user at the front end can create the product in my store. thanks alot for the help
Hello Dennis,
This code can be added where you need it. In your custom function. Actually the code above is enough to create a UI for users. But of course there would be HTML forms, JS etc.
Yes, it is possible with only JavaScript method but I have no ready code for it at this moment.
Hi, its great have a try.
Hi, I try to add new attribute values to an existing product (with existing variants) in order to be able to create new variants afterwards. With regard to your example, I try to add the values “small” and “very small”. After successfully doing so, the former values (Big, SuperBig) have vanished though. How can I add the new values rather than overwrite the old ones?
Thank you!
Regards, Michael