News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

Get user data via Joomla user ID

Started by Kuubs, February 10, 2020, 09:15:50 AM

Previous topic - Next topic

Kuubs

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

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

should work
GJC Web Design
VirtueMart and Joomla Developers - php developers https://www.gjcwebdesign.com
VM4 AusPost Shipping Plugin - e-go Shipping Plugin - VM4 Postcode Shipping Plugin - Radius Shipping Plugin - VM4 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
https://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

Kuubs

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?

Studio 42

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).

Kuubs

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?

Studio 42

Perhaps you disabled it or the filed name is not right(compare with the other fields name)

Kuubs

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


?>

Studio 42

Curious way to set the datas. Perhaps you do it wrong.

Kuubs

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

Studio 42

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();

Kuubs

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.

Studio 42

Dump the values on the model user > store function to check if you get your datas.(use vmdebug for eg.)

Kuubs

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?

Studio 42

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

is it being filtered out by the bind / check functions of the store process?
GJC Web Design
VirtueMart and Joomla Developers - php developers https://www.gjcwebdesign.com
VM4 AusPost Shipping Plugin - e-go Shipping Plugin - VM4 Postcode Shipping Plugin - Radius Shipping Plugin - VM4 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
https://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation