- Posts: 8
- Thank you received: 3
Custom User Plugin - Hikashop Address
12 years 11 months ago #125937
by Billymb3
Custom User Plugin - Hikashop Address was created by Billymb3
-- url of the page with the problem -- : Localhost - Testing
-- HikaShop version -- : HikaShop Business: 2.2.1
-- Joomla version -- : 3.1.5
-- PHP version -- : 5.3.8
-- Browser(s) name and version -- : Not Applicable
-- Error-message(debug-mod must be tuned on) -- : Lots so far
Hello, I am looking for a little bit of help. I have tried a few different components for managing users that integrate with Hikashop and have not liked what was available for various reasons. I created a user component that allows me to customize the Login page, Registration page, and Profile pages more the way I want them. I have also created a User Plugin so I can choose what fields show on all of these pages (address1, address 2, email, etc.)
I am now working on the Profile Page and would like to show the Hikashop address instead of the Joomla one. I am using code from the standard user - profile plugin in my own and it works like the original shows address1 and updates the table #__user_profiles when updating the user information.
The original onContentPrepareData function is as follows.
After doing a few searches I am close but have not been able to come up with the best query to get the hikashop (address_street and address_street2 from the table #__hikashop_address) and inject it into the $data->profile array.
I believe I have to get the user_id from #__hikashop_user where the field user_cms_id matches the current users id.
And then use this user_id to query the #__hikashop_address table and get the address_street and address_street2 fields that match the current user.
Does anyone know if I can write one query to get all the user_profile info, and the hikashop_address info?
After that query is figured out I need to figure out how to inject it all into the $data->profile array that the user profile plugin will display for editing in the profile, and also change the onUserAfterSave function to update both the hikashop_address table and user_profile table.
Any help would be great, I am ok with standard sql queries but these ones are making my head spin.
-- HikaShop version -- : HikaShop Business: 2.2.1
-- Joomla version -- : 3.1.5
-- PHP version -- : 5.3.8
-- Browser(s) name and version -- : Not Applicable
-- Error-message(debug-mod must be tuned on) -- : Lots so far
Hello, I am looking for a little bit of help. I have tried a few different components for managing users that integrate with Hikashop and have not liked what was available for various reasons. I created a user component that allows me to customize the Login page, Registration page, and Profile pages more the way I want them. I have also created a User Plugin so I can choose what fields show on all of these pages (address1, address 2, email, etc.)
I am now working on the Profile Page and would like to show the Hikashop address instead of the Joomla one. I am using code from the standard user - profile plugin in my own and it works like the original shows address1 and updates the table #__user_profiles when updating the user information.
The original onContentPrepareData function is as follows.
Code:
public function onContentPrepareData($context, $data)
{
// Check we are manipulating a valid form.
if (!in_array($context, array('com_users.profile', 'com_users.user', 'com_users.registration', 'com_usermod.profile', 'com_usermod.notlogged', 'com_admin.profile')))
{
return true;
}
if (is_object($data))
{
$userId = isset($data->id) ? $data->id : 0;
if (!isset($data->profile) and $userId > 0)
{
// Load the profile data from the database.
$db = JFactory::getDbo();
$db->setQuery(
'SELECT profile_key, profile_value FROM #__user_profiles' .
' WHERE user_id = ' . (int) $userId . " AND profile_key LIKE 'profile.%'" .
' ORDER BY ordering'
);
try
{
$results = $db->loadRowList();
}
catch (RuntimeException $e)
{
$this->_subject->setError($e->getMessage());
return false;
}
// Merge the profile data.
$data->profile = array();
foreach ($results as $v)
{
$k = str_replace('profile.', '', $v[0]);
$data->profile[$k] = json_decode($v[1], true);
if ($data->profile[$k] === null)
{
$data->profile[$k] = $v[1];
}
}
}
if (!JHtml::isRegistered('users.url'))
{
JHtml::register('users.url', array(__CLASS__, 'url'));
}
if (!JHtml::isRegistered('users.calendar'))
{
JHtml::register('users.calendar', array(__CLASS__, 'calendar'));
}
if (!JHtml::isRegistered('users.tos'))
{
JHtml::register('users.tos', array(__CLASS__, 'tos'));
}
}
return true;
}
After doing a few searches I am close but have not been able to come up with the best query to get the hikashop (address_street and address_street2 from the table #__hikashop_address) and inject it into the $data->profile array.
I believe I have to get the user_id from #__hikashop_user where the field user_cms_id matches the current users id.
Code:
// Get a db connection.
$hikashop_user_id = JFactory::getDbo();
// Create a new query object.
$hikashop_query = $hikashop_user_id->getQuery(true);
$hikashop_user_id->setQuery('SELECT user_id, user_cms_id FROM #__hikashop_user WHERE user_cms_id = ' . (int) $userId);
// Set $hikashop_id as the id we matched up in the hikashop_user_id query
$hikashop_id = $hikashop_user_id_results['user_id'];
And then use this user_id to query the #__hikashop_address table and get the address_street and address_street2 fields that match the current user.
Code:
// Load the users hikashop address that matches the correct id
$hikashop_db = JFactory::getDbo();
$hikashop_db->setQuery('SELECT address_id, address_user_id FROM #__hikashop_address WHERE address_user_id =' . (int) $hikashop_id);
Does anyone know if I can write one query to get all the user_profile info, and the hikashop_address info?
After that query is figured out I need to figure out how to inject it all into the $data->profile array that the user profile plugin will display for editing in the profile, and also change the onUserAfterSave function to update both the hikashop_address table and user_profile table.
Any help would be great, I am ok with standard sql queries but these ones are making my head spin.
Please Log in or Create an account to join the conversation.
12 years 11 months ago #126065
by nicolas
Replied by nicolas on topic Custom User Plugin - Hikashop Address
Hi,
You should do like that:
You should do like that:
Code:
// Get a db connection.
$db = JFactory::getDbO();
$db->setQuery('SELECT user_id FROM #__hikashop_user WHERE user_cms_id = ' . (int) $userId);
$hikashop_id = $db->loadResult();
$db->setQuery('SELECT * FROM #__hikashop_address WHERE address_user_id =' . (int) $hikashop_id);
$address = $db->loadObject();
//then you can do like that to get the address_street for example:
echo $address->address_street;
Please Log in or Create an account to join the conversation.
12 years 11 months ago #126076
by Billymb3
Replied by Billymb3 on topic Custom User Plugin - Hikashop Address
Nicolas, thank you so much! I tried it out and it is working perfectly! Now time to make some more coffee and continue on with this little adventure.
Cheers!
Cheers!
Please Log in or Create an account to join the conversation.
Less
More
- Posts: 6
- Thank you received: 0
12 years 3 months ago #160031
by Fit_webmaster
Replied by Fit_webmaster on topic Custom User Plugin - Hikashop Address
Did not want to open a new ticket for it as it is very similar... I am trying to get the address from the registration form too, but I it not coming through. I am even getting a empty array. 
function onAfterUserCreate(&$element){
$user_id = $element->user_id;
$db = JFactory::getDbO();
$db->setQuery('SELECT * FROM #__hikashop_address WHERE address_user_id =' . (int) $user_id);
$address = $db->loadObjectList();
JLog::add('User ID: ' print_r($user_id, true));
JLog::add('Address Obj: '.print_r($address, true));
JLog::add('Street is: '.print_r($address->address_street, true));
JLog::add('Email is: '.print_r($element->user_email, true));
}
Output
User ID: 153
Address Obj: Array ( )
Street is:
Email is: 123@test.com
Any Idea? This is driving me insane.
function onAfterUserCreate(&$element){
$user_id = $element->user_id;
$db = JFactory::getDbO();
$db->setQuery('SELECT * FROM #__hikashop_address WHERE address_user_id =' . (int) $user_id);
$address = $db->loadObjectList();
JLog::add('User ID: ' print_r($user_id, true));
JLog::add('Address Obj: '.print_r($address, true));
JLog::add('Street is: '.print_r($address->address_street, true));
JLog::add('Email is: '.print_r($element->user_email, true));
}
Output
User ID: 153
Address Obj: Array ( )
Street is:
Email is: 123@test.com
Any Idea? This is driving me insane.
Please Log in or Create an account to join the conversation.
12 years 3 months ago #160032
by Xavier
Replied by Xavier on topic Custom User Plugin - Hikashop Address
Hi,
The code seem to be correct. The issue is maybe due to the fact that the user is created before the address, so when getting data in "onAfterUserCreate()", the address is potentially not yet created, that's why you get an empty array.
You maybe have to use an address trigger, like "onAfterAddressCreate()".
The code seem to be correct. The issue is maybe due to the fact that the user is created before the address, so when getting data in "onAfterUserCreate()", the address is potentially not yet created, that's why you get an empty array.
You maybe have to use an address trigger, like "onAfterAddressCreate()".
The following user(s) said Thank You: Fit_webmaster
Please Log in or Create an account to join the conversation.
Less
More
- Posts: 6
- Thank you received: 0
12 years 3 months ago #160330
by Fit_webmaster
Replied by Fit_webmaster on topic Custom User Plugin - Hikashop Address
That was it!
Thanks so much for replying/helping.
Cheers Xavier
Thanks so much for replying/helping.
Cheers Xavier
Please Log in or Create an account to join the conversation.
Time to create page: 0.243 seconds