向WooCommerce订单添加额外数据API(WP-JSON)

发布于 2025-01-21 08:35:12 字数 962 浏览 0 评论 0原文

当我消耗WP-JSON/WC/V2/订单端点时,我得到的结果类似于以下结果,

[{
"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 protected]",
    "phone": ""
},
..........

我需要在“ billing ”部分中添加一个字段,在此结果中,此字段将仅针对此API生成。 WooCommerce是否提供了这样做的过滤器?

When I consume the wp-json/wc/v2/orders endpoint, I get a result similar to the following

[{
"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 protected]",
    "phone": ""
},
..........

I need to add one more field in the "billing" part in this result, this field would be generated only for this API. Does Woocommerce provide a filter to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蔚蓝源自深海 2025-01-28 08:35:12
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;
}
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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文