where is my mistake in a source with custom options? im working with woocommerce rest api. In foreach I need add different option like S – blue, M – red, S – red, M – blue, but I got empty inputs in wordpress:
there is my code and DOC:
http://woothemes.github.io/woocommerce-rest-api-docs/#create-a-product
public function addProduct($data)
{
$wc_api = $this->_getClient();
// size
$sizeArray = array();
foreach($data['size'] as $size){
$sizeArray[] = $size;
}
// color
$colorArray = array();
foreach($data['color'] as $color){
$colorArray[] = $color;
}
foreach ($data['size'] as $size) {
foreach ($data['color'] as $color) {
$options[] =
[
'regular_price' => $data['price'],
'attributes' =>
[
array('name' => 'Size', 'slug' => 'size', 'option' => $size),
array('name' => 'Color', 'slug' => 'color', 'option' => $color)
]
];
}
}
// http://woothemes.github.io/woocommerce-rest-api-docs/#create-a-product
$newProductData = array(
'product' => array(
'title' => $data['title'],
'type' => 'variable',
'regular_price' => $data['price'],
'description' => $data['description'],
'sku' => $data['sku'],
'tags' => [ $data['tags'] ],
'attributes' =>
[
array('name' => 'Size', 'slug' => 'size', 'position' => '0', 'visible' => true, 'variation' => true, 'options' => [ implode(' | ', $sizeArray) ]),
array('name' => 'Color', 'slug' => 'color', 'position' => '1', 'visible' => true, 'variation' => true, 'options' => [ implode(' | ', $colorArray) ])
],
'variations' => $options,
'images' => [ array('src' => $data['image'], 'position' => '0') ],
'virtual' => true
)
);
return $wc_api->create_product($newProductData);
}
I had the same issue when i was trying to create a product specially a variable product. Finally got a solution for it.
If your code is correct and its just not creating the variation then the solution i found is that you must use only one attribute ‘name’ or ‘slug’ with ‘option’ attribute kept as it is. Try using only ‘name’ and ‘option’ in the attributes or ‘slug’ and ‘option’.
I am giving my code for as an example here.
For this example i am using only the first attribute i.e [0] position as an demo.
My point is that- ‘name’ and ‘slug’ won’t work together. Use anyone of them. This solution worked for me. Do try lemme know whether it worked/useful for you.