Api Does Not Return Newly Created Profile Fields

Hi!

I added some profile fields to store some further information of the user.

Unfortunalely the API does not return these newly created profiles fields when performing a GET request on

https://example.com/api/users/203

A newly created field is defined in cscart_profile_fields, the value itself is stored in cs_cart_profile_fields_data.

How can I get the API to return these fields too?
I appreciate any support!
Many greetings
hummer

app/Tygh/Api/Entities/Users.php

try to add

$data['fields'] = fn_get_profile_fields_data('U', $id);

after

$data = reset($data);

Then clear cache

(!) Not tested

Cool, eComLabs, works!

{
    "user_id": "203",
[...]
    "anonymized": null,
    "fields": {
        "60": "custom01_value",
        "61": "custom02_value"
    }
}
Some thoughts on this:

- Is there a possibility to get the "field_name" instead of the "field_id" (i.e. instead of "60" e.g. "my_custom_field_01")?

- This is a change of core files, right? I am not release-safe with that, right? (there is no hook?!)?

Would be another option to extend the API with a new entity (e.g. "Userdetails") that returns the array of fn_get_profile_fields_data as "data"? Compare: https://docs.cs-cart.com/latest/developer_guide/addons/api_extending.html
Thanks for your help!

Best regards

hummer

1. It is not possible our of the box. But you can write your own function based on the fn_get_profile_fields_data one (app/functions/fn.users.php)

2. Right, there is no hook, unfortunately. As alternative, you can use post hook in the fn_get_users function and extend the data returned by this function. You can use the following condition to check if API is used

if (defined('API')) {
    ... your code here....
}

3. It also possible, but point #2 is easier

In the meantime I have implemented #3 (add a new entity to the API). It works so far, (but) it now takes two API calls to fully receive a user's data.
https://example.com/api/users/203
https://example.com/api/usersdetails/203
So I'll take a closer look at #2.
Thank you very much, eComLabs. Thank you for the time and effort you take so often.
Best regards
hummer

You are welcome! :)

So I'll take a closer look at #2.

I took a closer look at #2 (establishing a POST hook get_users_post). Here's my code:

if ( !defined(‘AREA’) ) { die(‘Access denied’); }

function fn_my_changes_get_users_post($users, $params, $auth)
{

if (defined('API')) {

	$user_id = $params['user_id'];
	
	$users = reset($users);
	$users['userdetails'] = fn_get_profile_fields_data('U', $user_id); 
			
	echo '
'; print_r($users); echo '
'; }

}

?>

Array push (with $users['userdetails']) obviously works as echo of print_r shows.

But in the API response "userdetails" are missing (red marked). What I am doing wrong?

[attachment=14832:postman_get_user.PNG]

Looking forward to any support!

Best regards

hummer

postman_get_user.PNG

Just replace

function fn_my_changes_get_users_post($users, $params, $auth)

with

function fn_my_changes_get_users_post(&$users, $params, $auth)

Now I get as API response

"203"

after changing the function header to

function fn_my_changes_get_users_post(&$users, $params, $auth)

[attachment=14833:postman_get_user_02.PNG]

Any further idea?

postman_get_user_02.PNG

Please try

function fn_my_changes_get_users_post(&$users, $params, $auth)
{
if (defined('API') && !empty($users)) {
    foreach ($users as $k => $v) {
        $users[$k]['userdetails'] = fn_get_profile_fields_data('U', $v['user_id']);
    }
}
}

Sure, I have to iterate that associative array using foreach as it can contain several users.

Embarrassing on my part. :-/

Thank you, eComLabs!

Sure, I have to iterate that associative array using foreach as it can contain several users.

Embarrassing on my part. :-/

Thank you, eComLabs!

You are welcome! :)