Author Topic: Get user data via Joomla user ID  (Read 1731 times)

Huubs

  • Jr. Member
  • **
  • Posts: 236
Get user data via Joomla user ID
« on: February 10, 2020, 09:15:50 am »
Hello,

I want to add a userfield with an image path so users can upload logo to their profile. But I'm struggling with how to accomplish this. When the user is logged in I can get the Joomla ID from the user, but how can I then get the Shop user data? So I can get their user data and fill in the correct field I created? Is there some sort of function for it?

GJC Web Design

  • 3rd party VirtueMart Developer
  • Super Hero
  • *
  • Posts: 9782
  • Virtuemart, Joomla & php developer
    • GJC Web Design
  • VirtueMart Version: 3.6.8
Re: Get user data via Joomla user ID
« Reply #1 on: February 10, 2020, 11:23:21 am »
$userModel = VmModel::getModel('user');
$userDetails = $userModel->getUser();

should work
GJC Web Design
VirtueMart and Joomla Developers - php developers http://www.gjcwebdesign.com
VM3 AusPost Shipping Plugin - e-go Shipping Plugin - VM3 Postcode Shipping Plugin - Radius Shipping Plugin - VM3 NZ Post Shipping Plugin - AusPost Estimator
Samport Payment Plugin - EcomMerchant Payment Plugin - ccBill payment Plugin
VM2 Product Lock Extension - VM2 Preconfig Adresses Extension - TaxCloud USA Taxes Plugin - Virtuemart  Product Review Component
http://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

Huubs

  • Jr. Member
  • **
  • Posts: 236
Re: Get user data via Joomla user ID
« Reply #2 on: February 10, 2020, 14:50:45 pm »
Thanks that works. But now how can I set a certain field.

I tried using this:

Code: [Select]
$userModel = VmModel::getModel('user');
$userDetails = $userModel->getUser();

$userDetails->userInfo[1]->imagepath = 'path/to/file.jpg';


But it's not saving the data. is there a function I need to use?

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 4367
  • Joomla & Virtuemart developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3
Re: Get user data via Joomla user ID
« Reply #3 on: February 10, 2020, 16:18:33 pm »
You should add a field imagepath in the shopperfields
YOURSITE/administrator/index.php?option=com_virtuemart&view=userfields
then you add a script so the user can  upload an image for eg. Or you do a Virtuemart shopperfield plugin (vmuserfield).

Huubs

  • Jr. Member
  • **
  • Posts: 236
Re: Get user data via Joomla user ID
« Reply #4 on: February 10, 2020, 16:31:29 pm »
You should add a field imagepath in the shopperfields
YOURSITE/administrator/index.php?option=com_virtuemart&view=userfields
then you add a script so the user can  upload an image for eg. Or you do a Virtuemart shopperfield plugin (vmuserfield).

I have done that. The field is already there. But when I run the script I see it filled in, but it doesn't save the actual data... So I thought maybe it's protected or something?

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 4367
  • Joomla & Virtuemart developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3
Re: Get user data via Joomla user ID
« Reply #5 on: February 11, 2020, 17:40:01 pm »
Perhaps you disabled it or the filed name is not right(compare with the other fields name)

Huubs

  • Jr. Member
  • **
  • Posts: 236
Re: Get user data via Joomla user ID
« Reply #6 on: February 13, 2020, 11:56:14 am »
Perhaps you disabled it or the filed name is not right(compare with the other fields name)

Nope haven't disabled it. I just tested if I could change other fields via this method, and that is also not possible. For example first_name isn't changed when I change it via code:

Code: [Select]
<?php

$userModel 
VmModel::getModel('user');
$userDetails $userModel->getUser();

$userData array_keys($userDetails->userInfo);


$userDetails->userInfo[$userData[0]]->imagepath '/images/virtuemart/'.$form->data['file'];

$userDetails->userInfo[$userData[0]]->first_name $form->data['name'];


echo 
"Uploaded logo: <img src='".$userDetails->userInfo[$userData[0]]->imagepath."' 
 style='width:200px; height:auto' /><br />"
// shows the uploaded logo

echo $userDetails->userInfo[$userData[0]]->first_name//shows the input name


?>

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 4367
  • Joomla & Virtuemart developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3
Re: Get user data via Joomla user ID
« Reply #7 on: February 13, 2020, 13:25:29 pm »
Curious way to set the datas. Perhaps you do it wrong.

Huubs

  • Jr. Member
  • **
  • Posts: 236
Re: Get user data via Joomla user ID
« Reply #8 on: February 17, 2020, 13:07:52 pm »
Curious way to set the datas. Perhaps you do it wrong.

What do you mean by this? How can I do it the right way??

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 4367
  • Joomla & Virtuemart developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3
Re: Get user data via Joomla user ID
« Reply #9 on: February 17, 2020, 21:58:47 pm »
Get directly the information form table
Code: [Select]
   $user = JFactory::getUser();
   $db = JFactory::getDBO();
   $q = 'SELECT * FROM `#__virtuemart_userinfos` WHERE `virtuemart_user_id` = "' . (int)$user->id.'" AND `address_type`= "BT"';
   $db->setQuery($q);
   $userInfos = $db->loadObject();

Huubs

  • Jr. Member
  • **
  • Posts: 236
Re: Get user data via Joomla user ID
« Reply #10 on: February 18, 2020, 12:58:33 pm »
Get directly the information form table
Code: [Select]
   $user = JFactory::getUser();
   $db = JFactory::getDBO();
   $q = 'SELECT * FROM `#__virtuemart_userinfos` WHERE `virtuemart_user_id` = "' . (int)$user->id.'" AND `address_type`= "BT"';
   $db->setQuery($q);
   $userInfos = $db->loadObject();

The loading of data goes well, only the saving does not. So you advice to just directly change the values in the database itself? I was hoping Virtuemart had some kind of native function for this.

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 4367
  • Joomla & Virtuemart developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3
Re: Get user data via Joomla user ID
« Reply #11 on: February 18, 2020, 21:31:10 pm »
Dump the values on the model user > store function to check if you get your datas.(use vmdebug for eg.)

Huubs

  • Jr. Member
  • **
  • Posts: 236
Re: Get user data via Joomla user ID
« Reply #12 on: February 19, 2020, 10:00:16 am »
Dump the values on the model user > store function to check if you get your datas.(use vmdebug for eg.)

I have dumped the values, but I see the values coming back in that object. But if I use the store function nothing happens. I activated the error handling in the backend, but that is not displaying anything. Is there another way to debug this ?

Can you show me in code what I must do to achieve what you are saying?

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 4367
  • Joomla & Virtuemart developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3
Re: Get user data via Joomla user ID
« Reply #13 on: February 19, 2020, 14:00:58 pm »
Sorry, but it need to many time to do more test. But i never had problem to do it.
Else do your own vmuserfield plugin, so it's more flexible.

GJC Web Design

  • 3rd party VirtueMart Developer
  • Super Hero
  • *
  • Posts: 9782
  • Virtuemart, Joomla & php developer
    • GJC Web Design
  • VirtueMart Version: 3.6.8
Re: Get user data via Joomla user ID
« Reply #14 on: February 19, 2020, 15:15:32 pm »
is it being filtered out by the bind / check functions of the store process?
GJC Web Design
VirtueMart and Joomla Developers - php developers http://www.gjcwebdesign.com
VM3 AusPost Shipping Plugin - e-go Shipping Plugin - VM3 Postcode Shipping Plugin - Radius Shipping Plugin - VM3 NZ Post Shipping Plugin - AusPost Estimator
Samport Payment Plugin - EcomMerchant Payment Plugin - ccBill payment Plugin
VM2 Product Lock Extension - VM2 Preconfig Adresses Extension - TaxCloud USA Taxes Plugin - Virtuemart  Product Review Component
http://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation