Rest Api Get / Update Order Custom Field for Woocommerce

0
78
views
You can extend the Rest API response with some code like:

add_action( ‘rest_api_init’, ‘your_custom_register_api_fields’ );

function your_custom_register_api_fields() {

        register_rest_field( ‘shop_order’,’your_custom_field‘,
        array(
            ‘get_callback’    => ‘your_custom_api_meta_callback’,
            ‘update_callback’ => null,
            ‘schema’          => null,
        )
        );
}
function your_custom_api_meta_callback( $post, $field_name, $request ) {
    // Update your_custom_field Code Start
    if (isset($request[‘your_custom_field’])) {
        update_post_meta( $post[ ‘id’ ], ‘your_custom_field’, stripslashes( $request[‘your_custom_field’] ) );
    }
    // Update your_custom_field Code End
    return get_post_meta( $post[ ‘id’ ], $field_name, true );
}
Advertisement


Thanks for reading my blog. I hope it will help you, Please leave your comments here if it’s really help you.   

Thank You ,
Mayank Zalavadia