- Posts: 628
- Thank you received: 18
Problem with hikashop API
4 years 6 months ago #340042
by patoushka
Problem with hikashop API was created by patoushka
-- HikaShop version -- : 4.5.0
-- Joomla version -- : 3.10.2
-- PHP version -- : 7.4
HI,
I'm currently developing a project to import hikashop users.
But I got a problem when import multiple users in a loop. Here is an example of the code:
The problem is caused by the address. When I insert the address, it works fine but when the address is supposed to be updated, it's not working. Only the last user address is updated.
Do you know why?
Thanks,
-- Joomla version -- : 3.10.2
-- PHP version -- : 7.4
HI,
I'm currently developing a project to import hikashop users.
But I got a problem when import multiple users in a loop. Here is an example of the code:
Code:
foreach ($clients as $client) {
try {
$user = $userClass->get($client["email"], "email");
$app = Factory::getApplication();
$app->setUserState( HIKASHOP_COMPONENT.'.user_id', $user->user_id);
if(!empty($client["billingAddress"])) {
$this->addOrUpdateAddress($client["billingAddress"], "billing", $user->user_id);
}
if(!empty($client["shippingAddress"])) {
$this->addOrUpdateAddress($client["shippingAddress"], "shipping", $user->user_id);
}
} catch (\Exception $ex) {
}
}
private function addOrUpdateAddress($address, $type, $user_id)
{
$addressClass = hikashop_get('class.address');
$addresses = array_values($addressClass->getByUser($user_id));
$index = array_search($type, array_column($addresses, 'address_type'));
$addressData = new stdClass();
if($index !== false) {
$addressData = (object)$addresses[$index];
}
$addressData->address_type = $type;
$addressData->address_user_id = $user_id;
$addressData->address_street = $address["street"];
$addressData->address_city = $address["city"];
$addressData->address_post_code = $address["zipCode"];
$addressData->address_telephone = $address["phone"];
$status = $addressClass->save($addressData);
}
The problem is caused by the address. When I insert the address, it works fine but when the address is supposed to be updated, it's not working. Only the last user address is updated.
Do you know why?
Thanks,
Please Log in or Create an account to join the conversation.
4 years 6 months ago #340046
by nicolas
Replied by nicolas on topic Problem with hikashop API
Hi,
There are many checks in the save function of class.address
For example, if you're calling it from the frontend, it checks that only the user already linked to the address has the right to modify it.
To circumvent that check, I can see that you tried to add:
so that the hikashop_loadUser() function used in the save function to know the current user would return the id of the user for which you're updating the address.
However, if you look at the code of the hikashop_loadUser() function, you'll see that the system will only look at the userState if the user data isn't already loaded in the static variable.
So you need to reset the static variable each time, or the hikashop_loadUser function will just return the data from the static variable, ignoring the userState. You can do so by add that code:
That way, next time the hikashop_loadUser function is called from the save function of class.address its static variable will be empty and it will use the userState to load the user data and the check in the save function will think that the current user is matching with the user_id of the address being updated.
There are many checks in the save function of class.address
For example, if you're calling it from the frontend, it checks that only the user already linked to the address has the right to modify it.
To circumvent that check, I can see that you tried to add:
Code:
$app->setUserState( HIKASHOP_COMPONENT.'.user_id', $user->user_id);
However, if you look at the code of the hikashop_loadUser() function, you'll see that the system will only look at the userState if the user data isn't already loaded in the static variable.
So you need to reset the static variable each time, or the hikashop_loadUser function will just return the data from the static variable, ignoring the userState. You can do so by add that code:
Code:
hikashop_loadUser(false, true);
Please Log in or Create an account to join the conversation.
4 years 6 months ago #340065
by patoushka
Replied by patoushka on topic Problem with hikashop API
Thanks a lot for the fast reply, that solved my problem.
The following user(s) said Thank You: Philip
Please Log in or Create an account to join the conversation.
Time to create page: 0.185 seconds