| +34 608 61 64 10



Prestashop, how to add code promotion to register form

I have to add two promotional code, where the user create your account in my Prestashop application (note: I'm using the 1.5 version). The first one is that identifies the user owner of it (I'll call it, codepromopadre) that invited the user to my application and the second one is the user code (I'll call it, codepromouser) that identify the user and that he/she could pass to new users to connect to my application.
Every new users, will have to introduce the codepromopadre only during the first register.
So, I have to modify these parts:

- Template
- Controller
- Customer table and, of course, the model

First of all, Prestashop has these steps in orde to create a user, as we can find into the method postProcess of the AuthController:

These are my steps:
1) Modify themes/MYTHEME/authentication.tpl
1a) After Email Address field, add the HTML input for the new field codepromopadre

<label for="codepromopadre_create">{l s='Code Promotion'}</label>
<span><input type="text" id="codepromopadre_create" name="codepromopadre_create" value="{if isset($}{$|stripslashes}{/if}" class="account_input" /></span>

1b) Modify the AJAX call adding this code

codepromopadre_create: $('#codepromopadre_create').val(),

1c) I have to add this information into the second step of form register. In this case I add also the own user code that I have got in a parameter table (I'll explain it later)
So, I modify the method SubmitCreate in this way

<p class="required text">
<label for="codepromopadre">{l s='El codigo de promocion'} <sup>*</sup></label>
<input type="text" readonly class="text" id="codepromopadre" name="codepromopadre" value="{if isset($}{$}{/if}" />
<p class="required text">
<label for="codepromouser">{l s='Tu codigo'} <sup>*</sup></label>
<input type="text" readonly class="text" id="codepromouser" name="codepromouser" value="{if isset($}{$}{/if}" />

Note: I define readonly the two fiels (and the email), cause I prefer that the user doesn't change it. But, of course, it should be simple to check them again.

2) Modify controllers/AuthController.php
Where the controller get the code data, (method processSubmitCreate) I add, this line


At this point I check if the codepromopadre exist, using my custom method codepromopadreExists and if it's ok, I assign a code to the new user, using another custom method getCodepromo, while if it doesn't exist, I send a standard error message, like this

$this->errors[] = Tools::displayError('MYMESSAGE', false);

Then I assign my two new variables to POST array:

$_POST['codepromopade'] = $codepromopadre;
$_POST['codepromouser'] = $codepromouser;

Now, I assign the value to the cookie, into the last method SubmitLogin


The last modify into the controller, is adding the codepromouser to the email that I'll send to user, into the method sendConfirmationMail:

'{promocodeuser}' => $customer->promocodeuser,

4) Modify the Model classes/customer.php
I define these two new variables, that I'll insert into the customer table

/** @var string codepromopadre */
public $codepromopadre;
/** @var string codepromouser */
public $codepromouser;     

Then, adding this field into the array, with generic validation

'codepromopadre' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
'codepromouser' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'), 

Finally, I show the new function to check if the codepromopadre exists and to get the last available code to assing to the new user

public function codepromopadreExists($codepromopadre){
	$sql = 'SELECT `id_customer`
		FROM `'._DB_PREFIX_.'customer`
		WHERE `codepromouser` = "'.pSQL($codepromopadre).'"';
	$result = Db::getInstance()->getRow($sql);

        if (isset($result['id_customer']))
		return false;
	return true;

public function getCodepromo()
	$sql = 'SELECT MAX(`id_codepromo`) , 
                FROM  `customer_codepromo` 
                WHERE used =0';
	$result = Db::getInstance()->getRow($sql);
	return $result['value'];

This is the database script to create the two new fields and the new table

ALTER TABLE `customer` ADD `codepromopadre` VARCHAR( 32 ) NULL ,
ADD `codepromouser` VARCHAR( 32 ) NULL;

CREATE TABLE IF NOT EXISTS `customer_codepromo` (
  `id_codepromo` int(11)  unsigned NOT NULL AUTO_INCREMENT,
  `value` VARCHAR(32) NOT NULL,
  `used` tinyint(1) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id_codepromo`)