当我使用When /wc/v2/orders端点时,得到的结果如下所示
[{
"id": 744276,
"parent_id": 0,
"status": "on-hold",
"currency": "BRL",
"version": "6.4.0",
"prices_include_tax": false,
"date_created": "2022-04-14T11:46:29",
"date_modified": "2022-04-14T11:46:31",
"discount_total": "0.00",
"discount_tax": "0.00",
"shipping_total": "0.00",
"shipping_tax": "0.00",
"cart_tax": "0.00",
"total": "45.59",
"total_tax": "0.00",
"customer_id": 1,
"order_key": "wc_order_iDHRxaUAWKKMS",
"billing": {
"first_name": "Name",
"last_name": "Last",
"company": "",
"address_1": "",
"address_2": "",
"city": "",
"state": "",
"postcode": "",
"country": "BR",
"email": "email@email.com",
"phone": ""
},
..........
在这个结果中,我需要在“记帐”部分中再添加一个字段,这个字段将只为这个API生成。Woocommerce提供了一个过滤器来完成这个任务吗?
发布于 2022-04-14 21:20:49
add_action('rest_api_init', 'order_custom_fields');
function order_custom_fields() {
register_rest_field(
'shop_order',
'custom_fields', //field name in JSON response
array(
'get_callback' => 'get_order_custom_fields', // custom function name
'update_callback' => null,
'schema' => null,
)
);
}
function get_order_custom_fields($object, $field_name, $request) {
$custom_data = get_post_meta($object['id'], '_billing_email', true);
$object['billing']['additional'] = $custom_data;
return $object;
}
https://stackoverflow.com/questions/71873735
复制相似问题