向WooCommerce订单API(wp-json)添加额外数据



当我使用wp-json/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生成。Wooccommerce提供了一个过滤器吗?

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;
}

最新更新