Jump to content

  • You cannot start a new topic
  • You cannot reply to this topic

Api Does Not Return Newly Created Profile Fields Rate Topic   - - - - -

 
  • hummer
  • Advanced Member
  • Members
  • Join Date: 23-Jul 20
  • 75 posts

Posted 19 January 2021 - 07:33 AM #1

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

 

 



 
  • eComLabs
  • CS-Cart Expert
  • Authorized Reseller
  • Join Date: 27-Jan 14
  • 21567 posts

Posted 19 January 2021 - 01:37 PM #2

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


GET A FREE QUOTE | CS-Cart Add-ons | CS-Cart Licenses | CS-Cart Development | CS-Cart Design | Server Configuration | UniTheme and YOUPI
CS-Cart                USD 345     Multi-Vendor              USD 1250    Multi-Vendor PLUS           USD 3100 (2775)
CS-Cart Ultimate  USD 775     CS-Cart + YOUPI      USD 545      Multi-Vendor Ultimate       USD 7500 (6000)

 
  • hummer
  • Advanced Member
  • Members
  • Join Date: 23-Jul 20
  • 75 posts

Posted 19 January 2021 - 03:19 PM #3

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..._extending.html
 
Thanks for your help!

 

Best regards

hummer



 
  • eComLabs
  • CS-Cart Expert
  • Authorized Reseller
  • Join Date: 27-Jan 14
  • 21567 posts

Posted 20 January 2021 - 01:36 PM #4

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


GET A FREE QUOTE | CS-Cart Add-ons | CS-Cart Licenses | CS-Cart Development | CS-Cart Design | Server Configuration | UniTheme and YOUPI
CS-Cart                USD 345     Multi-Vendor              USD 1250    Multi-Vendor PLUS           USD 3100 (2775)
CS-Cart Ultimate  USD 775     CS-Cart + YOUPI      USD 545      Multi-Vendor Ultimate       USD 7500 (6000)

 
  • hummer
  • Advanced Member
  • Members
  • Join Date: 23-Jul 20
  • 75 posts

Posted 21 January 2021 - 12:01 PM #5

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


 
  • eComLabs
  • CS-Cart Expert
  • Authorized Reseller
  • Join Date: 27-Jan 14
  • 21567 posts

Posted 21 January 2021 - 01:41 PM #6

You are welcome! :)


GET A FREE QUOTE | CS-Cart Add-ons | CS-Cart Licenses | CS-Cart Development | CS-Cart Design | Server Configuration | UniTheme and YOUPI
CS-Cart                USD 345     Multi-Vendor              USD 1250    Multi-Vendor PLUS           USD 3100 (2775)
CS-Cart Ultimate  USD 775     CS-Cart + YOUPI      USD 545      Multi-Vendor Ultimate       USD 7500 (6000)

 
  • hummer
  • Advanced Member
  • Members
  • Join Date: 23-Jul 20
  • 75 posts

Posted 25 January 2021 - 01:17 PM #7

 

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:

 

<?php

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 '<pre>'; print_r($users); echo '</pre>'; 
		
	}
	
}

?>

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?

 

Attached File  postman_get_user.PNG   48.07KB   0 downloads

 

Looking forward to any support!

 

Best regards

hummer



 
  • eComLabs
  • CS-Cart Expert
  • Authorized Reseller
  • Join Date: 27-Jan 14
  • 21567 posts

Posted 25 January 2021 - 01:20 PM #8

Just replace

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

with

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

GET A FREE QUOTE | CS-Cart Add-ons | CS-Cart Licenses | CS-Cart Development | CS-Cart Design | Server Configuration | UniTheme and YOUPI
CS-Cart                USD 345     Multi-Vendor              USD 1250    Multi-Vendor PLUS           USD 3100 (2775)
CS-Cart Ultimate  USD 775     CS-Cart + YOUPI      USD 545      Multi-Vendor Ultimate       USD 7500 (6000)

 
  • hummer
  • Advanced Member
  • Members
  • Join Date: 23-Jul 20
  • 75 posts

Posted 25 January 2021 - 01:27 PM #9

Now I get as API response

"203"

after changing the function header to 

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

Attached File  postman_get_user_02.PNG   23.76KB   0 downloads

 

Any further idea?



 
  • eComLabs
  • CS-Cart Expert
  • Authorized Reseller
  • Join Date: 27-Jan 14
  • 21567 posts

Posted 26 January 2021 - 04:34 AM #10

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']);
    }
}
}

GET A FREE QUOTE | CS-Cart Add-ons | CS-Cart Licenses | CS-Cart Development | CS-Cart Design | Server Configuration | UniTheme and YOUPI
CS-Cart                USD 345     Multi-Vendor              USD 1250    Multi-Vendor PLUS           USD 3100 (2775)
CS-Cart Ultimate  USD 775     CS-Cart + YOUPI      USD 545      Multi-Vendor Ultimate       USD 7500 (6000)

 
  • hummer
  • Advanced Member
  • Members
  • Join Date: 23-Jul 20
  • 75 posts

Posted 26 January 2021 - 08:01 AM #11

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

Embarrassing on my part. :-/

 

Thank you, eComLabs!



 
  • eComLabs
  • CS-Cart Expert
  • Authorized Reseller
  • Join Date: 27-Jan 14
  • 21567 posts

Posted 26 January 2021 - 01:49 PM #12

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! :)


GET A FREE QUOTE | CS-Cart Add-ons | CS-Cart Licenses | CS-Cart Development | CS-Cart Design | Server Configuration | UniTheme and YOUPI
CS-Cart                USD 345     Multi-Vendor              USD 1250    Multi-Vendor PLUS           USD 3100 (2775)
CS-Cart Ultimate  USD 775     CS-Cart + YOUPI      USD 545      Multi-Vendor Ultimate       USD 7500 (6000)