【发布时间】:2015-04-13 15:18:13
【问题描述】:
我在带有自定义选项的源中的错误在哪里?我正在使用 woocommerce rest api。在 foreach 中,我需要添加不同的选项,例如 S - blue、M - red、S - red、M - blue,但我在 wordpress 中得到了空输入:
有我的代码和文档: 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);
}
【问题讨论】:
标签: php wordpress api woocommerce