Problem with onAfterAddressCreate()

  • Posts: 128
  • Thank you received: 2
8 years 3 months ago #246535

-- HikaShop version -- : 2.6.3
-- Joomla version -- : 3.6.0
-- PHP version -- : 5.5.30

Hi,

I just wrote a plugin to pass some of the details in the billing address to the Community Builder profile by using the onAfterAddressCreate(&$element) event.

The plugin works fine, however it only works if I set the option "ask address on registration = NO" (configuration->checkout->Login and Registration). However if I set "ask address on registration = YES" the plugin does not work, even though the hikashop address is created in the database.

I think it does not work because when the option "ask address on registration = YES" is used, the plugin gets called before the Community Builder user is created. However when using "ask address on registration = NO" once the hikashop address is created the community builder user already exists.

Is there a way to get the event triggered in the right moment?

alternatively, instead of updating a community builder user from my plugin I could create a new one. will this cause a problem if hikashop tries to create the same community builder user later?

Last edit: 8 years 3 months ago by sabroso.

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
8 years 3 months ago #246546

Hi,

You're right, the address in HikaShop is created before the user is added to CB.
The problem is that normally, it should be the role of CB to register the user on his end when the Joomla user creation trigger is called (like we do, and like all the other extensions do). But here, we have to do it ourselves after the registration process of HikaShop.

What I would recommend is simple. Call the addAndConfirmUserInCB function from class.user in your plugin in the onAfterAddressCreate function.
That way, you'll add the user yourself if it's missing in CB regardless of what we change in HikaShop in the future, so it's a much safer method.
If you look in that function, the MySQL query used is a REPLACE, so that means that even if you create the user yourself in CB, it won't be a problem if HikaShop recreates it after you. HikaShop's query will just replace your row in the CB table.

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

  • Posts: 128
  • Thank you received: 2
8 years 3 months ago #246712

Thanks Nicolas,

I tried the following:

					
			if (!$cbUserExists)
			{
				$newUser = new stdClass;
				$newUser->user_id = $element->address_user_id;
				$newUser->user_cms_id = $this->find_user_id($element->address_user_id);;
				hikashopUserClass::addAndConfirmUserInCB($newUser);	
			}

But ended up with the following error:
Strict Standards: Non-static method hikashopUserClass::addAndConfirmUserInCB() should not be called statically, assuming $this from incompatible context in /opt/lampp/htdocs/joomla/plugins/hikashop/mihikashop/mihikashop.php on line 32

Notice: Undefined property: plgHikashopMihikashop::$database in /opt/lampp/htdocs/joomla/administrator/components/com_hikashop/classes/user.php on line 708

Fatal error: Call to a member function Quote() on a non-object in /opt/lampp/htdocs/joomla/administrator/components/com_hikashop/classes/user.php on line 708

How should I call the addAndConfirmUserInCB function from class.user from my plugin in the onAfterAddressCreate function?

Thanks

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
8 years 3 months ago #246734

Hi,

Like we usually call class functions in HikaShop:

$userClass = hikashop_get('class.user');
$userClass->addAndConfirmUserInCB($newUser);

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

  • Posts: 128
  • Thank you received: 2
8 years 3 months ago #246769

Thanks, now I can call addAndConfirmUserInCB from my plugin.

But there is a problem with the REPLACE query in your addAndConfirmUserInCB function. It deletes all the changes I apply to the CB user. It happens like this:

1. onAfterAddressCreate triggers my plugin
2. I trigger addAndConfirmUserInCB in my plugin and the CB user is created correctly
3. I trigger my function update_cb_user() in my plugin so I update the just created CB user with information from the just created hikashop address ( for example City and Country ) -> this works correctly, I can see if I stop the execution here
4. After my plugin finishes then Hikashop triggers addAndConfirmUserInCB() again as per the normal Hikashop workflow and the REPLACE query deletes all the extra information my plugin added (City, Country, etc).

So I end up in the same situation that I would be if my plugin didn't do anything since I lose all the extra info that my plugin adds to the CB user.

I'm not sure what your suggested solution would be but I can think of a couple of possibilities:
a) That Hikashop triggers addAndConfirmUserInCB() right AFTER creating the Joomla/Hikashop user and BEFORE the address is created.
b) It may make sense to move your addAndConfirmUserInCB() to a plugin so it is outside of Hikashop core and can be switched ON/OFF. This will make sense for people who do not use CB at all and for people who want to integrate CB with Hikashop in a different way
c) Add a check within addAndConfirmUserInCB() to see if the user already exists in CB and if so then exit the function

I'm all ears if you can think of a better/simpler solution

many thanks

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
8 years 3 months ago #246802

Hi,

That's indeed problematic if you want to add stuff in the same row of CB.
I thought that you just needed the id of the user in CB and in that case, it would have been good enough.

Reading what you're saying, if I understand correctly, you get the city/state/country values from the HikaShop user address and store it with the user in CB in your plugin. Wouldn't that be something we could actually do by default in the addAndConfirmUserInCB function of HikaShop ? That way, it would be a nice improvement for everyone.
Moving the synchronization with CB outside of HikaShop and adding it as a plugin is a good idea. We could actually merge it with the CB plugin we have here:
www.hikashop.com/support/documentation/7...mmunity-builder.html
But yes, adding a check in addAndConfirmUserInCB to not add the user if it is already in CB would be a good thing too.

Instead of a REPLACE, we could do first a SELECT based on the user_cms_id and then if no entry was found, we would do an INSERT, and if found, we wouldn't do anything.

Here is the modified function to add the check:

function addAndConfirmUserInCB($newUser, $addressData = null) {
		
		$query = 'SELECT id FROM #__comprofiler WHERE id='.(int)$newUser->user_cms_id;
		$this->database->setQuery($query);
		$CBID = $this->database->loadResult();
		if($CBID){
			return true;
		}
		
		if(is_null($addressData)) {
			$addressClass = hikashop_get('class.address');
			$addresses = $addressClass->getByUser($newUser->user_id);
			$addressData = reset($addresses);
		}

		$fields = array(
			'cbactivation' => $this->database->Quote(''),
			'id' => (int)$newUser->user_cms_id,
			'user_id' => (int)$newUser->user_cms_id,
			'approved' => 1,
			'confirmed' => 1
		);

		if(!empty($addressData->address_firstname))
			$fields['firstname'] = $this->database->Quote($addressData->address_firstname);

		if(!empty($addressData->address_middle_name))
			$fields['middlename'] = $this->database->Quote($addressData->address_middle_name);

		if(!empty($addressData->address_lastname))
			$fields['lastname'] = $this->database->Quote($addressData->address_lastname);

		$query = 'INSERT INTO #__comprofiler (' . implode(',', array_keys($fields)) . ') VALUES (' . implode(',', $fields) . ')';
		$this->database->setQuery($query);
		$this->database->query();
		
		return true;
	}
Could you test it ?

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

  • Posts: 128
  • Thank you received: 2
8 years 3 months ago #246864

Hi,

Yes, what I'm doing in my plugin is populating extra details into the CB profile from the Hikashop address and some other default values that I set after user registration.

The issue I see with extending the current addAndConfirmUserInCB function is that different people will have different fields in their CB profiles, so probably it will make more sense to do a plugin with params where the mapping of fields can be specified. But I see this as a bigger job.

For now, I have tested your suggested changes for the addAndConfirmUserInCB function and it works well. Now my plugin integrates well with Hikashop and I get the extra fields across to the CB profile.

I have also tested the registration process with my plugin disabled and I can see that Hikashop behaves in the same way as it did with the previous addAndConfirmUserInCB. So I see no risk in applying the suggested changes.

My question now is, considering this is a core hack in Hikashop, will you apply this changes in future versions of Hikashop so I can keep upgrading easily to new versions?

Many thanks.

Last edit: 8 years 3 months ago by sabroso.

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
8 years 3 months ago #246888

Hi,

Of course. Now that you have tested it, we can safely add it for the next version of HikaShop.

A mapping of the fields would indeed be a bigger job. If you happen to do it, I know a few people who would be interested so it's definitely an interesting thing.

The following user(s) said Thank You: sabroso

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

Time to create page: 0.058 seconds
Powered by Kunena Forum