我目前正在使用以下扩展wp-json REST API,它增加了两个额外的字段:
add_action( 'rest_api_init', 'create_api_posts_meta_field' );
function create_api_posts_meta_field() {
register_rest_field( 'shop_order', 'user_meta', array('get_callback' => 'get_post_meta_for_api', 'schema' => null));
register_rest_field( 'shop_order', 'user_shipping', array('get_callback' => 'get_shipping_for_api', 'schema' => null));
}
function get_post_meta_for_api( $object ) {
$post_id = $object['id'];
$user = get_user_by( 'ID', $object['customer_id'] );
unset($user->data->user_pass);
return $user->data;
}
function get_shipping_for_api( $object ) {
$post_id = $object['id'];
$the_order = wc_get_order( $post_id );
foreach( $the_order->get_items('fee') as $item_id => $item_fee ){
$fee_name = $item_fee->get_name();
if($fee_name == 'Shipping'){
$fee_total = $item_fee->get_amount();
}
}
return $fee_total;
}
这在身份验证和访问时应能正常工作:
http://mywebsite.com/wp-json/wc/v2/orders/{order_id}.
但是我需要像这样访问Legacy API:
http://mywebsite.com/wc-api/v2/orders/{order_id}.
同样,我可以很好地访问旧版API,但是我扩展api的功能似乎无效。
应该行吗?还是有替代方法来扩展旧版API。