Jump to content

how can i add a custom field in registration


Recommended Posts

hello evereyone 

I want to add a custom field at the registration level (for example: ID card number)

my prestashop version: 1.7.6.3

note: I have already seen this but I cannot install this module ( https://www.h-hennes.fr/blog/2017/10/10/prestashop-1-7-ajouter-des-champs-clients )

thank's !

Link to comment
Share on other sites

On 2/3/2020 at 9:34 PM, karim31 said:

hello evereyone 

I want to add a custom field at the registration level (for example: ID card number)

my prestashop version: 1.7.6.3

note: I have already seen this but I cannot install this module ( https://www.h-hennes.fr/blog/2017/10/10/prestashop-1-7-ajouter-des-champs-clients )

thank's !

In order to add fields in Customer form, You need to register below hooks  and add column with field name in customer table 

For adding new field on form 

additionalCustomerFormFields

you can add new fields by creating FormField object 

        $extra_fields['hobbies'] = (new FormField)
            ->setName('name of field')
            ->setType('type of field text, select or any other')
            ->setValue('value for field')
            ->setLabel('Label');

     validateCustomerFormFields

add validation for the field you added in form

actionObjectCustomerUpdateAfter

save value in database

actionObjectCustomerAddAfter

save value in database

This link can help you a bit more

https://devdocs.prestashop.com/1.7/modules/sample_modules/grid-and-identifiable-object-form-hooks-usage/

Link to comment
Share on other sites

Sample function for the hook : additionalCustomerFormFields (Adds the hobbies field)

/**
 * Add fields in Customer Form
 *
 * @param array $params parameters (@see CustomerFormatter->getFormat())
 *
 * @return array of extra FormField
 */
public function hookAdditionalCustomerFormFields($params)
{
  $module_fields = $this->readModuleValues();

  $hobbies_value = Tools::getValue('hobbies');
  if (isset($module_fields['hobbies'])) {
  	$hobbies_value = $module_fields['hobbies'];
  }

  $extra_fields = array();
  $extra_fields['hobbies'] = (new FormField)
    ->setName('hobbies')
    ->setType('text')
    ->setValue($hobbies_value)
    ->setLabel($this->l('Hobbies'));

  return $extra_fields;
}

 

Sample function for the hook : validateCustomerFormFields (Dance, Shopping or Yoga only allowed)

/**
 * Validate fields in Customer form
 *
 * @param array $params hook call parameters (@see CustomerForm->validateByModules())
 *
 * @return array of extra FormField
 */
public function hookvalidateCustomerFormFields($params)
{
	$module_fields = $params['fields'];
	if ('Dance' != $module_fields[0]->getValue()
		&& 'Shopping' != $module_fields[0]->getValue()
		&& 'Yoga' != $module_fields[0]->getValue()
	) {
		$module_fields[0]->addError(
			$this->l('Only "Dance", "Shopping" or "Yoga"')
		);
	}
	return array(
		$module_fields
	);
}

 

 

Link to comment
Share on other sites

Ok thank you for the information, because once I tried to make some customization without the field declaration in the class (and without using the hooks mentioned above)
just field in the database and extra field in the admin controller UI and the data could not be inserted in the db extra column.

Link to comment
Share on other sites

  • 1 year later...
On 2/5/2020 at 11:42 AM, fbenoist.com said:

hookAdditionalCustomerFormFields and hookvalidateCustomerFormFields

Hi 

i need add two simple fields to the registration customer form (zipcode and phone_mobile) and save it in ps_customer table , i know i can override CustomerFormatter.php  function getFormat() , or add hookAdditionalCustomerFormFields and hookvalidateCustomerFormFields but i dont know rules for prestashop overrides and where find this hooks or add .. i m trying to find a tutorial for this ..anyone know where can i find it ?? in version prestahop 1.7.7.1 

i think i need these walkthrough :

1- Add fields to form registration

2- validate these fields 

3 - Add these fields to table ps_customer

Edited by victorweb
more info (see edit history)
Link to comment
Share on other sites

 

I have managed to manage the extra fields in the front and save in the database but I can't get it out in the back, I have seen in tutorials override on AdminCustomerController.php but in version 1.7.7.1 I can't find that controller, i find admincustomerthreadscontroller ... someone can help ?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...