Creating address for newly created user

  • Posts: 30
  • Thank you received: 5
3 months 2 weeks ago #358097

-- HikaShop version -- : 5.0.2
-- Joomla version -- : 4.4.1
-- PHP version -- : 8.1.26
-- Browser(s) name and version -- : Firefox 121.0 (64-bit)

Hi,

I made a scheduled task plugin that regularly checks a file and automatically creates new users if needed.

Now, I also need to create HikaShop addresses for these users and I am a bit lost.

I've noticed that HikaShop users are automatically created once Joomla users are created.

So my logic would be this:
1) Get the Hika user by email
2) Create new address using the information from my list
3) Add new address to user

I've gotten as far as including HikaShop's main files and loading the 'user' and 'address' classes.

Sadly, I can't find an overview of which functions I can work with to make the above happen. Where can I find documentation of these classes/Which functions should I use?

Thanks in advance!

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

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
3 months 2 weeks ago #358099

Hi,

To get the user based on the email, you can do like this:

$user = $userClass->get($email_address, 'email');

To create a new address, you can do like this:
$address = new stdClass();
$address->address_user_id = $user->user_id;
$address->address_type = 'billing';
$address->address_published = 1;
$address->address_firstname = $firstname;
$address->address_lastname = $lastname;
...
$addressClass->save($address);

Note also that in address_state and address_country, you need to provide the namekey of the zone in the hikashop_zone table.
For example:
$country = "france";
$zoneClass = hikashop_get('class.zone');
$country_zone = $zoneClass->getZones(array($country), '*', 'zone_name_english', true);
$address->address_country = $country_zone['zone_namekey'];

You can infer this from looking directly at the code of the classes in the files of the folder administrator/components/com_hikashop/classes/
When you do hikashop_get('class.product') it basically returns you an object of the class in the file product.php of that folder.
The point of using hikashop_get instead of a require is that it's shorter to write, and it also handles dynamic overrides of the classes.

The following user(s) said Thank You: NTV

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

  • Posts: 30
  • Thank you received: 5
3 months 2 weeks ago #358127

Hi nicolas,

got it to work - many thanks!

Some things I had to change.

The value of $address_country is supposed to look like this: 'country_Germany_81'.

Since my input values are in german I have to use 'zone_name' as a key instead of 'zone_name_english'. Thankfully, my HikaShop installation is either smart enough or german as well, since this returns the right zone:

$zone_id = $zoneClass->getZones(array($user_data['country']), 'zone_id', 'zone_name', true);
$zone_name_english = $zoneClass->getZones(array($user_data['country']), 'zone_name_english', 'zone_name', true);
$address_country = 'country';
$address_country .= '_' . $zone_name_english[0];
$address_country .= '_' . $zone_id[0];

Two things I'm not quite happy with.
1) I'd rather have only one database call. But calling getZones() with two columns (or '*' as you put it) seems to return only the zone_id. I also tried setting $returnArrayWithOneColumn to false, but I'm guessing that is for something else.
2) Do I need to do that string formatting for $address_country myself or is there a HikaShop function that already does it (if I give it the zone_id for example)?

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

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
3 months 2 weeks ago #358128

Hi,

1. You can set returnArrayWithOneColumn to false. In that case, you'll get an object and you can do:
$returnedObject->zone_namekey
to access the attributes.

2. The zone_namekey column should already contain the string formatted like you want. That's why I provided the code which uses the zone_namekey from the database.

The following user(s) said Thank You: NTV

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

  • Posts: 30
  • Thank you received: 5
3 months 2 weeks ago #358152

Hi,

you might laugh, but what tripped me up was that getZones() returns an array, so I simply had to do this:

$zones = $zoneClass->getZones(array($user_data['country']), 'zone_namekey', 'zone_name', true);
$address->address_country = $zones[0];

In conclusion: print_r() and logging is your friend.

Thanks again, you've been a great help as always.

The following user(s) said Thank You: nicolas

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

Time to create page: 0.060 seconds
Powered by Kunena Forum