I tried to create products in woo-commerce using API with product variations but it gives error like authentication failed.
If I remove variations from API data it works product successfully added into woo-commerce.
Below is actual code which I have used.
$data = '{
"name": "'.$Name.'",
"type": "simple",
"regular_price": "'.$Price.'",
"sku": "'.$Code.'",
"description": "'.$Description.'",
"categories": [
{
"id": "'.$CategoryID.'"
}
],
"attributes": [
{
"name": "Pos Id",
"visible": true,
"variation":true,
"options": [
"'.$Id.'"
]
}
],
"variations" : [
{
[
"visible": true,
"name" : "Pos Id",
"regular_price" : "'.$Price.'",
"slug" : "pos id",
"option" :[
"'.$Id.'"
]
]
}
]
}';
echo $url = 'http://www.xxxxxxxxx.xxx/wp-json/wc/v1/products/'.$param;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
if(curl_error($ch))
{
echo "in if";
echo 'error:' . curl_error($ch);
}else{
echo"<pre>";
print_r(json_decode($response));
Please help me with what is wrong in above code.
Read more here: How to add variations to product in woo-commerce using API