Custom field default valie

  • Posts: 22
  • Thank you received: 2
  • Hikashop Business
1 month 1 week ago #359830

-- HikaShop version -- : 5.0.3
-- Joomla version -- : 5.0.3

Hi.
is there any way that I can set the default value of an checkout custom field in hikashop by user's username ?
for example if the user's username is XYZ i want the default value of my field to be XYZ

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

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
1 month 1 week ago #359838

Hi,

It might be possible.
Could you provide a screenshot of the settings of the custom field ?
Could you also provide the URL of the shop ?
And finally, could you explain why you're trying to do that ?
Based on your anwsers, we might be able to provide a solution, or an alternative.

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

  • Posts: 22
  • Thank you received: 2
  • Hikashop Business
1 month 1 week ago #359914

I have created an extra fields for the first name and last name of the person who will recieve the order.
I need some code that when the user is completing the checkout form, the default value of this field be the name of his/her account until he wants to change it

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

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
1 month 1 week ago #359918

Hi,

I'm happy to help but you need to answer to my questions first.
Could you provide a screenshot of the settings of the custom field ? Based on how the custom field is configured, the code to use will be different. I can't provide any code if you don't provide the settings of the custom fields.
Could you also provide the URL of the shop ? This is to be able to better understand how your checkout is configured in order to do this. It can also affect how the code needs to be written.
Why having a custom field with the first name and last name of the user in another custom field than the default first name and last name custom fields of the address would help in your case ?

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

  • Posts: 22
  • Thank you received: 2
  • Hikashop Business
2 weeks 1 day ago #360293

i dont want to add another custom field. i want to use first name and last name custom fields which is already created by default.

let me explain more.

when a user is buying a product, i'm asking the first name and last name of the person who the product will be delivered to, in most cases, the person who is recieving the product is the person that is buying the product, which i already have his/her name in joomla users, ok ?

i want the first and last name custom field to be pre-filled by the user's name, so that he/she dosent need to fill that fields in the address form again unless he wants to change the name.

is it clear now ?

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

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
2 weeks 3 hours ago #360300

Hi,

Thanks, it's clearer. However, I still am not sure why you need this.

Let's take the example of our demo website. If you add something to the cart and go to the checkout, on the registration form you can see this: i.imgur.com/atq3L2b.png
So, if I understand, you would like that on this form, when someone enters something in the "name" field at the top, it automatically fill the first name a last name fields. Is that correct ?
While not impossible, this would require adding custom javascript code to the registration form.

In this case, I would recommend a simpler approach. If you set the "registration" setting to "simplified registration" or "simplified registration with password" instead of "registration" under the Checkout tab of the HikaShop configuration, then the name field will be removed from that form.
Instead, the name of the Joomla user will be filled automatically with the first name and last name entered in the address.

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

  • Posts: 22
  • Thank you received: 2
  • Hikashop Business
1 week 3 days ago #360390

i thought it would be easy by creating some overrides.
getting user data in php file and set it as default value of the field

i have doubt that i coudnt explain well what i need ..

thank you by the way

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

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
1 week 3 days ago #360395

Hi,

If you look at the example I gave you, the user is not yet logged in and the firstname and lastname fields are visible.
So this means that in this case it is impossible to get the name of the user account to prefill the firstname and lastname fields of the form, since that information is not yet known to your server. It's a matter of order of the events.
That's what I was trying to explain with my example.
In this case, the only solution would be to do it with javascript code on the fly on the page.
This is still possible to add with a view override.
And if the user is logged in and you access the checkout, then the user will be logged in, and you can indeed use the information in PHP to prefill the fields, also with a view override. But normally, when the user accesses the checkout after adding a product to the cart, he is not yet logged in. And thus, you can do this.
Also, another point I didn't brought up in my previous message is that the name of the user in Joomla is in one field. And the firstname and lastname in the address are two different fields. Splitting the name from Joomla in two can be hazardous. What happens if the user enters only one word in there ? What if he enters 3 words ? What if his name is in Chinese characters without any spaces between the firstname and the lastname ? What about the order of the words ? In some countries the firstname is first when writting the full name, and in some others, the lastname comes first. Doing it the other way, by combining the firstname and lastname from the address to generate name of the user account during its creation avoids all these pitfalls. And best of all, it doesn't require any coding on your end since it's just an option to change.
Technically, you could have a mixed solution of both PHP and javascript in order to handle all this properly. If you're a developer, it's not difficult to do. You'll just spend a few hours on it or more. It's not just one or two lines of code. So since you asked about the code to do it in your initial message, I assumed you were not a developer and thus I directed you to the simplified registration option.

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

  • Posts: 22
  • Thank you received: 2
  • Hikashop Business
1 week 1 day ago #360452

suppore that user is logged in, then what I need is possible ?
can i get the user data and set it as the default value of custom fields ?

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

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
1 week 1 day ago #360458

Hi,

If the user is already logged in when he sees the address form, then you can indeed prefill the firstname and lastname fields of the address with the name of the user. Still, it requires splitting the text in the name.

You'll want to edit the view file show_block_address via the menu Display>Views.
For example, after the line:

$this->edit_address = new stdClass();
you could add something like that:
if(emtpy($this->edit_address->address_firstname)) {
  $user = JFactory::getUser();
  $this->edit_address->address_firstname = $user->name;
}

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

  • Posts: 22
  • Thank you received: 2
  • Hikashop Business
6 days 2 hours ago #360479

thank you very much
i think I should have told you from the begining that the user is already logged in !! sorry for that and thank you

The following user(s) said Thank You: Philip

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

Time to create page: 0.074 seconds
Powered by Kunena Forum