Php 创建产品变体的WooCommerce API未按预期工作
在使用Php 创建产品变体的WooCommerce API未按预期工作,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,在使用/wp json/wc/v2/products//variances/batchrest v2 API端点时,将属性/属性术语链接到变体时遇到问题。() 在创建链接到全局属性的变体时,我们如何从API获得正确的响应。(下面代码的底部有那个API调用和响应) 您不能在使用WooCommerce rest V2 API的属性名称中使用“属性” 这不是一个常见的问题,但是这个例子不起作用,因为它生成了一个带有_属性的slug <?php require_once 'wooapi/vendo
/wp json/wc/v2/products//variances/batch
rest v2 API端点时,将属性/属性术语链接到变体时遇到问题。()
在创建链接到全局属性的变体时,我们如何从API获得正确的响应。(下面代码的底部有那个API调用和响应)
您不能在使用WooCommerce rest V2 API的属性名称中使用“属性”
这不是一个常见的问题,但是这个例子不起作用,因为它生成了一个带有_属性的slug
<?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>';
["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"
}
}