我已经从wordpress'网站上使用woocommerce rest api获取了json数据,响应如下:
[{"id":4077,"name":"Work Benches","slug":"work-benches","parent":4076,"description":"","display":"default","image":null,"menu_order":0,"count":10,"_links":{"self":[{"href":"https:\/\/www.fynds.co.uk\/wp-json\/wc\/v3\/products\/categories\/4077"}],"collection":[{"href":"https:\/\/www.fynds.co.uk\/wp-json\/wc\/v3\/products\/categories"}],"up":[{"href":"https:\/\/www.fynds.co.uk\/wp-json\/wc\/v3\/products\/categories\/4076"}]}},{"id":4802,"name":"Work Lights","slug":"work-lights","parent":4073,"description":"","display":"default","image":null,"menu_order":0,"count":23,"_links":{"self":[{"href":"https:\/\/www.fynds.co.uk\/wp-json\/wc\/v3\/products\/categories\/4802"}],"collection":[{"href":"https:\/\/www.fynds.co.uk\/wp-json\/wc\/v3\/products\/categories"}],"up":[{"href":"https:\/\/www.fynds.co.uk\/wp-json\/wc\/v3\/products\/categories\/4073"}]}},{"id":4029,"name":"Work Safety Protective Equipment","slug":"work-safety-protective-equipment","parent":0,"description":"","display":"default","image":null,"menu_order":0,"count":53,"_links":{"self":[{"href":"https:\/\/www.fynds.co.uk\/wp-json\/wc\/v3\/products\/categories\/4029"}],"collection":[{"href":"https:\/\/www.fynds.co.uk\/wp-json\/wc\/v3\/products\/categories"}]}},{"id":5248,"name":"Yoga & Pilates Blocks","slug":"yoga-pilates-blocks","parent":4296,"description":"","display":"default","image":null,"menu_order":0,"count":1,"_links":{"self":[{"href":"https:\/\/www.fynds.co.uk\/wp-json\/wc\/v3\/products\/categories\/5248"}],"collection":[{"href":"https:\/\/www.fynds.co.uk\/wp-json\/wc\/v3\/products\/categories"}],"up":[{"href":"https:\/\/www.fynds.co.uk\/wp-json\/wc\/v3\/products\/categories\/4296"}]}},{"id":4556,"name":"Yoga & Pilates Mats","slug":"yoga-pilates-mats","parent":4296,"description":"","display":"default","image":null,"menu_order":0,"count":22,"_links":{"self":[{"href":"https:\/\/www.fynds.co.uk\/wp-json\/wc\/v3\/products\/categories\/4556"}],"collection":[{"href":"https:\/\/www.fynds.co.uk\/wp-json\/wc\/v3\/products\/categories"}],"up":[{"href":"https:\/\/www.fynds.co.uk\/wp-json\/wc\/v3\/products\/categories\/4296"}]}}]
但是我只需要特定的属性,例如(id,name,slug)。
我搜索了很多问题,但没有明确的答案, 减少传入的json数据以使应用程序运行更快的目的 更新 我尝试不 woocommerce rest api
https://*myweb*/wp-json/wp/v2/posts?filter[posts_per_page]=10&fields=id,title
它起作用了 但不适用于woocommerce rest api:
https://www.***/wp-json/wc/v3/products?consumer_key=***&consumer_secret=****&per_page=10&field=id,name
答案 0 :(得分:1)
实际上有一种方法,但是您需要在woocommerce中的functions.php
中写一个钩子;)
假设以下网址http://localhost/wp-json/wc/v3/products?fields=sku,name
技巧是fields
参数,该参数包含用逗号分隔的要检索字段的列表。
因此,您将这种行为与woocommerce挂钩……是这样的:
add_filter( 'woocommerce_rest_prepare_product_object', 'my_woocommerce_rest_prepare_product_object', 10, 3 );
function my_woocommerce_rest_prepare_product_object( $response, $object, $request ) {
$data = $response->get_data();
$newdata = [];
foreach ( explode ( ",", $request['fields'] ) as $field ) {
$newdata[$field] = $data[$field];
}
$response->set_data( $newdata );
return $response;
}
和... 瞧
{
"sku": "544392",
"name": "ACIDET 3 EN 1 GALON",
"_links": {
"self": [
{
"href": "http://localhost/wp-json/wc/v3/products/35809"
}
],
"collection": [
{
"href": "http://localhost/wp-json/wc/v3/products"
}
]
}
},
{
"sku": "544391",
"name": "DESTAPADOR CANIERIA LEJIA GEL SELLO ROJOGL",
"_links": {
"self": [
{
"href": "http://localhost/wp-json/wc/v3/products/35808"
}
],
"collection": [
{
"href": "http://localhost/wp-json/wc/v3/products"
}
]
}
},
{
"sku": "544389",
"name": "ACIDET 3 EN 1 950ML",
"_links": {
"self": [
{
"href": "http://localhost/wp-json/wc/v3/products/35807"
}
],
"collection": [
{
"href": "http://localhost/wp-json/wc/v3/products"
}
]
}
},
...
希望有帮助!!顺便说一句,我设计了该解决方案,深入阅读了源代码。
:)
答案 1 :(得分:0)
我在 PazFernando 代码中添加了一个 IF,以防万一您可以在不指定字段的情况下发送 get 调用。
add_filter( 'woocommerce_rest_prepare_product_object', 'my_woocommerce_rest_prepare_product_object', 10, 3 );
function my_woocommerce_rest_prepare_product_object( $response, $object, $request )
{
$data = $response->get_data();
$newdata = [];
if ($request['fields'] != null)
{
foreach ( explode ( ",", $request['fields'] ) as $field )
{
$newdata[$field] = $data[$field];
}
$response->set_data( $newdata );
}
return $response;
}