Support MB User Meta Use MB User Meta data in meta box Reply To: Use MB User Meta data in meta box

#14150
Anh TranAnh Tran
Keymaster

Hi,

Now I got it!

Do you have a field for user to select a product that associated with that user? I didn't see you mentioned about it above.

If you already have that, assuming the field ID is ag_product, then you need to modify the rest response to include product details. Here is a sample code that you can use (put it in your theme's functions.php file or your plugin file):

add_action( 'rest_api_init', function () {
    register_rest_field( 'user', 'to_quantity', array(
        'get_callback' => function( $user ) {
            $product = get_user_meta( $user['id'], 'ag_product', true );
            return get_post_meta( $product, 'to_quantity', true );
        },
    ) );
    register_rest_field( 'user', 'to_description', array(
        'get_callback' => function( $user ) {
            $product = get_user_meta( $user['id'], 'ag_product', true );
            return get_post_meta( $product, 'to_description', true );
        },
    ) );
    register_rest_field( 'user', 'to_advantages', array(
        'get_callback' => function( $user ) {
            $product = get_user_meta( $user['id'], 'ag_product', true );
            return get_post_meta( $product, 'to_advantages', true );
        },
    ) );
} );