首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >向WooCommerce Orders API添加额外数据(wp-json)

向WooCommerce Orders API添加额外数据(wp-json)
EN

Stack Overflow用户
提问于 2022-04-14 15:03:41
回答 1查看 116关注 0票数 1

当我使用When /wc/v2/orders端点时,得到的结果如下所示

代码语言:javascript
运行
复制
[{
"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提供了一个过滤器来完成这个任务吗?

EN

回答 1

Stack Overflow用户

发布于 2022-04-14 21:20:49

代码语言:javascript
运行
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71873735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档