Reputation: 1923
I am using Automattic\WooCommerce
to create a plugin for my website. The idea is to create a variable product with some information and then, create the variations of the product.
Creating the product is working fine
POST
to /products
has not been any problem
But when I try to create the variations, I keep getting error messages. The last one is:
Fatal error: Uncaught Automattic\WooCommerce\HttpClient\HttpClientException: Error: Invalid parameter(s): attributes [rest_invalid_param]
I am sending this array to
POST
to /products/$id/variations
, but it results in the error described above. I don't know which is the invalid param.
{
"date_created": "2017-10-21",
"description": "Some random description",
"sku": "",
"price": "0",
"visible": true,
"purchasable": true,
"virtual": true,
"manage_stock": false,
"stock_quantity": 1,
"in_stock": true,
"date_on_sale_from": "2017-10-21",
"date_on_sale_to": "2017-10-24",
"attributes": {
"id": 0,
"option": "No certificate"
}
}
Upvotes: 2
Views: 10700
Reputation: 873
Make sure that attribute values which are numbers , to be passed as a string. It's stupid but if you pass attribute values as numbers and not strings it shoots an error.
Upvotes: 2
Reputation: 91
I going to write just if somebody fall here.
The first step you have to do is create your variable product variable whit all data you need.
$prod = [
'name' => 'Name the product'
'type' => 'variable',
'status' => 'draft',
'sku' => 'valid-sku',
'regular_price' => '120',
'manage_stock' => 'false',
'stock_status' => 'instock',
'tax_status' => 'taxable',
'categories' => [[id => 1]],
'attributes' => [
[ //Brand
'id' => 5,
'position' => 1,
'visible' => false,
'variation' => false,
'options'=> ['Brand you want'],
'name' => 'Marca'
],[ //Gender
'id' => 3,
'position' => 2,
'visible' => false,
'variation' => false,
'options'=> 'male',
'name' => 'Gender'
]
]
];
Make the post
$wooProd = $woocommerce->post('products', $prod);
And then create the variation information
$variation = ['create' => [
'sku' => $variation['SKU'],
'manage_stock' => 'true',
'stock_quantity'=> 'SKU-VARIATON',
'regular_price' => '150',
'sale_price' => '120',
'attributes' => [[ //Brand
'id' => 5,
'position' => 1,
'visible' => false,
'variation' => false,
'options'=> ['Brand you want'],
'name' => 'Brand',
],[ //Gender
'id' => 3,
'position' => 2,
'visible' => false,
'variation' => false,
'options'=> 'Male',
'name' => 'Gender',
]
]
]];
And make the post
$woocommerce->post( "products/$wooProd->id/variations/batch", $variation_data );
This is everything. Maybe you want to make a For to make the variation data mor complex or something like that..
I hope this work or make some light in the path :) Best regards!
Upvotes: 1
Reputation: 65
You should change
/products/$id/variations
to:
/products/{id}/variations
Then pass the $id
as parameter in your function.
Upvotes: 0
Reputation: 822
your attributes parameter is invalid. You should pass the attributes like following :
{
"date_created": "2017-10-21",
"description": "Some random description",
"sku": "",
"price": "0",
"visible": true,
"purchasable": true,
"virtual": true,
"manage_stock": false,
"stock_quantity": 1,
"in_stock": true,
"date_on_sale_from": "2017-10-21",
"date_on_sale_to": "2017-10-24",
"attributes": [
{
"id": 0,
"option": "No certificate"
}
]
}
you can see the documentation of the woocommerce rest API for the variation.
Visit https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-product-variation
Upvotes: 3