Prestashop 1.5: add custom field in Customer Registration

Da JAVA a C# passando per PHP, SQL ed HTML
Avatar utente
dino
Messaggi: 16580
Iscritto il: mercoledì 30 novembre 2011, 18:21

Prestashop 1.5: add custom field in Customer Registration

Messaggio da dino »

This is a guide on howto add a custom field in the customer registration form in Prestashop 1.5. Modifications is for your reference: please take into account that your Prestashop installation may be different from mine because of a different template or minor software revision. In this modification I was unable to use the override files because some of them are encrypted (used by some 3rd parties modules) so I have changed base files.

Warning: made a backup of the file prior to modify them!

File "themes/NOME_DEL_TEMA/authentication.tpl "

Add this lines in the point where you want:

Codice: Seleziona tutto

<p class="text">
<label for="referralcode">{l s='Referral code'}</label>
<input name="referralcode" type="text" class="text" id="referralcode" />
</p>
Warning: probably it's necessary to repeat the same code in other section of the file (depending on template).

File "controllers/front/AuthController.php"

About line 390, find the code:

Codice: Seleziona tutto

// Preparing customer
$customer = new Customer();
$lastnameAddress = Tools::getValue('lastname');
$firstnameAddress = Tools::getValue('firstname');


After, add:

Codice: Seleziona tutto

$referralCode = Tools::getValue('referralcode');
About line 430, find the code:

Codice: Seleziona tutto

$customer->birthday = (empty($_POST['years']) ? '' : (int)$_POST['years'].'-'.(int)$_POST['months'].'-'.(int)$_POST['days']);
if (!Validate::isBirthDate($customer->birthday))
$this->errors[] = Tools::displayError('Invalid date of birth.');
After, add:

Codice: Seleziona tutto

$customer->referralcode = $referralCode;
Linea 520 (circa), cercare il codice:

Codice: Seleziona tutto

$customer->birthday = (empty($_POST['years']) ? '' : (int)$_POST['years'].'-'.(int)$_POST['months'].'-'.(int)$_POST['days']);
if (!Validate::isBirthDate($customer->birthday))
$this->errors[] = Tools::displayError('Invalid date of birth');
Di seguito aggiungere:

Codice: Seleziona tutto

$customer->referralcode = $referralCode;
About line 650, find the code:

Codice: Seleziona tutto

$this->context->customer = $customer;
$this->context->smarty->assign('confirmation', 1);
$this->context->cookie->id_customer = (int)$customer->id;
After, add:

Codice: Seleziona tutto

$this->context->cookie->customer_referralcode = $customer->referralcode;

File "class/Customer.php"

About line 30, find the code:

Codice: Seleziona tutto

public $id;
After, add:

Codice: Seleziona tutto

/** @var string Referralcode */
public $referralCode;

If you are using translation file, available on the folder "themes/NOME_DEL_TEMA/lang", add following two lines:

Codice: Seleziona tutto

$_LANG['authentication-create-account_1b4033814553ad8ffb709e3b5cc6ee2c'] = 'INSERIRE_QUI_LA_TRADUZIONE';
$_LANG['authentication_1b4033814553ad8ffb709e3b5cc6ee2c'] = 'INSERIRE_QUI_LA_TRADUZIONE';

In order to show the field in the customer list within the Administration section, please modify the file "controllers/admin/AdminCustomersController.php"

About line 143, find the code:

Codice: Seleziona tutto

  'connect' => array(
  'title' => $this->l('Last visit'),
  'width' => 100,
  'type' => 'datetime',
  'search' => false,
  'havingFilter' => true
  )
);
Replace it with the following:

Codice: Seleziona tutto

  'connect' => array(
  'title' => $this->l('Last visit'),
  'width' => 100,
  'type' => 'datetime',
  'search' => false,
  'havingFilter' => true
  ),
  'referralcode' => array(
  'title' => $this->l('Referral code'),
  'width' => 'auto',
  'search' => true
  )
);

Alter the database (if you are using the table suffix "ps" the table is `ps_customer`):

Codice: Seleziona tutto

ALTER TABLE `ps_customer` 
ADD `referralcode` VARCHAR( 100 )

How Neller indicate me, I have missed a step!
in classes/Customer.php
around line 160
public static $definition = array(
...
'passwd' => array('type' => self::TYPE_STRING, 'validate' => 'isPasswd', 'required' => true, 'size' => 32),
Below this line, add this line
'referralcode' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
Thank you Neller.
_____________________________
Working harder: http://www.dinofratelli.it
Listen House Music: https://www.dinobrosdj.it
Safety online https://omniadpi.it/

anuksamun
Messaggi: 3
Iscritto il: giovedì 4 luglio 2013, 19:49

Re: Prestashop 1.5: add custom field in Customer Registratio

Messaggio da anuksamun »

Hi, i did everything you said but i have a problem. I've wrote more in detail on the prestashop forum on the topic regarding custom fields. I've created a custom field named CNP (personal numerical code). In my account in front office, on my details, if i complete this field, press save and then reinter here, the field is blank, but in back office i can see it. I think it has sometign to to with smarty.post, because filed like name sand lastname or email are displayed fine and they have value="{$smarty.post.firstname}"
Based on that i added in identity.tpl the code:

<p class="text">
<label for="referralcode">{l s='CNP'}</label>
<input name="referralcode" type="text" class="text" id="referralcode" value="{$smarty.post.referralcode}" />

</p>

But it doesn t work... i thing it must be adde something somewere...but don;t know were...


Avatar utente
dino
Messaggi: 16580
Iscritto il: mercoledì 30 novembre 2011, 18:21

Re: Prestashop 1.5: add custom field in Customer Registratio

Messaggio da dino »

Probably you have missed something in "controllers/front/AuthController.php"
_____________________________
Working harder: http://www.dinofratelli.it
Listen House Music: https://www.dinobrosdj.it
Safety online https://omniadpi.it/

anuksamun
Messaggi: 3
Iscritto il: giovedì 4 luglio 2013, 19:49

Re: Prestashop 1.5: add custom field in Customer Registratio

Messaggio da anuksamun »

I;ve checked and rechecked...all the lines are there... I just want that in the identity page, in my account, to display the numbers i have entered...like the firstname, lastname and email...

gboli
Messaggi: 2
Iscritto il: martedì 22 aprile 2014, 10:43

Re: Prestashop 1.5: add custom field in Customer Registratio

Messaggio da gboli »

Hi,

This is a nice tutorial. I followed all the steps and it worked perfectly.

But is there a way to ensure that the newly added field is not compulsory such that the form can still be submitted without filling the referral code field.

i added the referral code field to the first part of the registration form in prestashop 1.5 just after the date of birth field but the form doesnt submit except the referral code field is filled.

I want the form to submit even when the referral code field is left blank.

Avatar utente
dino
Messaggi: 16580
Iscritto il: mercoledì 30 novembre 2011, 18:21

Re: Prestashop 1.5: add custom field in Customer Registratio

Messaggio da dino »

I thinkj that you have to work on the validation rules: 'validate' => 'isGenericName'. Try to remove this ;)
_____________________________
Working harder: http://www.dinofratelli.it
Listen House Music: https://www.dinobrosdj.it
Safety online https://omniadpi.it/

gboli
Messaggi: 2
Iscritto il: martedì 22 aprile 2014, 10:43

Re: Prestashop 1.5: add custom field in Customer Registratio

Messaggio da gboli »

@dino I have tried that and it didnt work. any ideas please

eduard02
Messaggi: 3
Iscritto il: lunedì 14 luglio 2014, 2:15

Re: Prestashop 1.5: add custom field in Customer Registratio

Messaggio da eduard02 »

@gboli, @dino same problem to me, any solution for this?

Thank you

Avatar utente
dino
Messaggi: 16580
Iscritto il: mercoledì 30 novembre 2011, 18:21

Re: Prestashop 1.5: add custom field in Customer Registratio

Messaggio da dino »

Please could you be more precise?
_____________________________
Working harder: http://www.dinofratelli.it
Listen House Music: https://www.dinobrosdj.it
Safety online https://omniadpi.it/

Rispondi