VirtueMart Forum

VirtueMart 2 + 3 + 4 => General Questions => Topic started by: Kuubs on February 10, 2020, 09:15:50 AM

Title: Get user data via Joomla user ID
Post by: Kuubs 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?
Title: Re: Get user data via Joomla user ID
Post by: GJC Web Design on February 10, 2020, 11:23:21 AM
$userModel = VmModel::getModel('user');
$userDetails = $userModel->getUser();

should work
Title: Re: Get user data via Joomla user ID
Post by: Kuubs on February 10, 2020, 14:50:45 PM
Thanks that works. But now how can I set a certain field.

I tried using this:


$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?
Title: Re: Get user data via Joomla user ID
Post by: Studio 42 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).
Title: Re: Get user data via Joomla user ID
Post by: Kuubs on February 10, 2020, 16:31:29 PM
Quote from: Studio 42 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).

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?
Title: Re: Get user data via Joomla user ID
Post by: Studio 42 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)
Title: Re: Get user data via Joomla user ID
Post by: Kuubs on February 13, 2020, 11:56:14 AM
Quote from: Studio 42 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)

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:

<?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


?>
Title: Re: Get user data via Joomla user ID
Post by: Studio 42 on February 13, 2020, 13:25:29 PM
Curious way to set the datas. Perhaps you do it wrong.
Title: Re: Get user data via Joomla user ID
Post by: Kuubs on February 17, 2020, 13:07:52 PM
Quote from: Studio 42 on February 13, 2020, 13:25:29 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??
Title: Re: Get user data via Joomla user ID
Post by: Studio 42 on February 17, 2020, 21:58:47 PM
Get directly the information form table
   $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();
Title: Re: Get user data via Joomla user ID
Post by: Kuubs on February 18, 2020, 12:58:33 PM
Quote from: Studio 42 on February 17, 2020, 21:58:47 PM
Get directly the information form table
   $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.
Title: Re: Get user data via Joomla user ID
Post by: Studio 42 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.)
Title: Re: Get user data via Joomla user ID
Post by: Kuubs on February 19, 2020, 10:00:16 AM
Quote from: Studio 42 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.)

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?
Title: Re: Get user data via Joomla user ID
Post by: Studio 42 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.
Title: Re: Get user data via Joomla user ID
Post by: GJC Web Design on February 19, 2020, 15:15:32 PM
is it being filtered out by the bind / check functions of the store process?
Title: Re: Get user data via Joomla user ID
Post by: Kuubs on February 19, 2020, 15:37:47 PM
Quote from: GJC Web Design on February 19, 2020, 15:15:32 PM
is it being filtered out by the bind / check functions of the store process?
How can I check?

I found a solution by just manipulating the DB directly, but I'm kinda wondering why the normal process isn't working like it should.