Custom User Plugin - Hikashop Address

  • Posts: 8
  • Thank you received: 3
10 years 6 months ago #125937

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

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

Attachments:

Please Log in or Create an account to join the conversation.

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
10 years 6 months ago #126065

Hi,

You should do like that:

// 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;

The following user(s) said Thank You: cb75ter, Billymb3

Please Log in or Create an account to join the conversation.

  • Posts: 8
  • Thank you received: 3
10 years 6 months ago #126076

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!

Please Log in or Create an account to join the conversation.

  • Posts: 6
  • Thank you received: 0
9 years 10 months ago #160031

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: This email address is being protected from spambots. You need JavaScript enabled to view it.

Any Idea? This is driving me insane.

Please Log in or Create an account to join the conversation.

  • Posts: 13201
  • Thank you received: 2322
9 years 10 months ago #160032

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 following user(s) said Thank You: Fit_webmaster

Please Log in or Create an account to join the conversation.

  • Posts: 6
  • Thank you received: 0
9 years 10 months ago #160330

That was it!

Thanks so much for replying/helping.

Cheers Xavier

Please Log in or Create an account to join the conversation.

Time to create page: 0.068 seconds
Powered by Kunena Forum