【发布时间】:2017-11-27 19:37:09
【问题描述】:
在使用/wp-json/wc/v2/products/<product_id>/variations/batch rest v2 API 端点时,我无法将属性/属性术语链接到变体。 (https://woocommerce.github.io/woocommerce-rest-api-docs/?php#batch-update-product-variations)
在创建链接到全局属性的变体时,我们如何从 API 获得正确的响应。 (下面的代码底部有那个 API 调用和响应)
<?php
require_once 'wooapi/vendor/autoload.php';
use Automattic\WooCommerce\Client;
$woocommerce = new Client(
'http://localhost/wordpress/',
'ck_44b92c00ea35e6cc59c89c29051bf67c22e0df3a',
'cs_dd833592a1ef7a00a82c1711fd455db2e4c5bd15',
[
'wp_api' => true,
'version' => 'wc/v2',
]
);
$attributes_response = $woocommerce->post('products/attributes/batch',
array (
'create' =>
array (
0 =>
array (
'name' => 'test attribute 1',
'slug' => 'test_attribute_1',
),
1 =>
array (
'name' => 'test attribute 2',
'slug' => 'test_attribute_2',
),
),
));
echo '$attributes_response';
echo '<pre>';
var_dump($attributes_response);
echo '</pre>';
$terms_response_1 = $woocommerce->post('products/attributes/'.$attributes_response['create'][0]['id'].'/terms/batch',
array (
'create' =>
array (
0 =>
array (
'name' => 'a',
),
1 =>
array (
'name' => 'b',
),
2 =>
array (
'name' => 'c',
),
),
));
$terms_response_2 = $woocommerce->post('products/attributes/'.$attributes_response['create'][1]['id'].'/terms/batch',
array (
'create' =>
array (
0 =>
array (
'name' => 'd',
),
1 =>
array (
'name' => 'e',
),
2 =>
array (
'name' => 'f',
),
),
));
$products_response = $woocommerce->post('products/batch',
array (
'create' =>
array (
0 =>
array (
'name' => 'Wilson Pro Overgrip 50 pack - Comfort - White',
'regular_price' => '60.00',
'description' => 'test',
'stock_quantity' => 0,
'manage_stock' => true,
'sale_price' => '30.00',
'date_on_sale_from' => '',
'date_on_sale_to' => '',
'images' =>
array (
),
'attributes' =>
array (
0 =>
array (
'id' => $attributes_response['create'][0]['id'],
'name' => 'test attribute 1',
'variation' => true,
'visible' => true,
'options' =>
array (
0 => 'b',
1 => 'c',
),
),
1 =>
array (
'id' => $attributes_response['create'][1]['id'],
'name' => 'test attribute 2',
'variation' => true,
'visible' => true,
'options' =>
array (
0 => 'e',
1 => 'f',
),
),
),
'type' => 'variable',
),
),
));
echo '$products_response';
echo '<pre>';
var_dump($products_response);
echo '</pre>';
$variations_response = $woocommerce->post('products/'.$products_response['create'][0]['id'].'/variations/batch',
array (
'create' =>
array (
0 =>
array (
'attributes' =>
array (
0 =>
array (
'id' => $attributes_response['create'][0]['id'],
'option' => 'b',
),
1 =>
array (
'id' => $attributes_response['create'][1]['id'],
'option' => 'e',
),
),
'manage_stock' => true,
'regular_price' => '20.00',
),
1 =>
array (
'attributes' =>
array (
0 =>
array (
'id' => $attributes_response['create'][0]['id'],
'option' => 'c',
),
1 =>
array (
'id' => $attributes_response['create'][1]['id'],
'option' => 'f',
),
),
'manage_stock' => true,
'regular_price' => '40.00',
),
),
));
echo '$variations_response';
echo '<pre>';
var_dump($variations_response);
echo '</pre>';
下面是上面$variations_response的相关部分。对于全局属性,id 不应为 0;而是实际的属性 id。
["attributes"]=>
array(2) {
[0]=>
array(3) {
["id"]=>
int(0)
["name"]=>
string(6) "test_1"
["option"]=>
string(1) "c"
}
[1]=>
array(3) {
["id"]=>
int(0)
["name"]=>
string(6) "test_2"
["option"]=>
string(1) "f"
}
}["attributes"]=>
array(2) {
[0]=>
array(3) {
["id"]=>
int(0)
["name"]=>
string(6) "test_1"
["option"]=>
string(1) "c"
}
[1]=>
array(3) {
["id"]=>
int(0)
["name"]=>
string(6) "test_2"
["option"]=>
string(1) "f"
}
}
【问题讨论】:
标签: php wordpress woocommerce