How to customize register and contact forms in PrestaShop?

user546774 picture user546774 · Apr 25, 2012 · Viewed 39.8k times · Source

I need to know how to customize my contact and register forms. How to add new fileds ( and ) and make the information from these fields required or not required.

I need to know which files I must edit for these forms...

I use prestashop 1.4.7.0

Answer

Paul Campbell picture Paul Campbell · Apr 26, 2012

This is really two separate questions as there are major differences in how you would handle each case.

Answer 1

For the registration form you can write a module which contains two hook handler functions. These will be:

public function hookCreateAccountForm() {}
public function hookCreateAccount($params) {}

The first function allows you to add additional fields to the registration form (by default these are inserted at the end of the form authentication.tpl, although you could move them all as a single group elsewhere). It should simply return the additional form html you require.

The second function provides you with two parameters to handle the account creation process. This is executed after the standard fields have been validated and the new customer has been created. Unfortunately you cannot do validation on your additional fields using this (you would need to either use javascript or override AuthController to perform your own authentication in the preProcess() member function). In one of my own custom modules for a site I have the following, for example:

public function hookCreateAccount($params)
{
  $id_lang = (int)Configuration::get('PS_LANG_DEFAULT');
  $customer = $params['newCustomer'];
  $address = new Address(Address::getFirstCustomerAddressId((int)$customer->id));
  $membership_number = $params['_POST']['membership_number'];
  ....
  ....
}

$params['newCustomer'] is a standard Prestashop element in the array and contains the newly created customer object. Your fields will be in the $params['_POST'] array - in my case it was an input field called membership_number.

Answer 2

For the contact form it's a whole lot more complicated I'm afraid. The simplest method for the html is to just hard-code your additional fields in the template file contact-form.tpl.

To actually process the form you will need to create an override for the controller by ceating a file called ContactController.php in /<web-root>/<your-optional-ps-folder>/override/controller containing something like:

<?php
class ContactController extends ContactControllerCore {

  function preProcess()
  {
    if (Tools::isSubmit('submitMessage'))
    {
      // The form has been submitted so your field validation code goes in here.
      // Get the entered values for your fields using Tools::getValue('<field-name>')
      // Flag errors by adding a message to $this->errors e.g.
      $this->errors[] = Tools::displayError('I haven't even bothered to check!');
    }
    parent::preProcess();
    if (Tools::isSubmit('submitMessage') && is_empty($this->errors))
    {
      // Success so now perform any addition required actions
      // Note that the only indication of success is that $this->errors is empty
    }
  }
}

Another method would be to just copy the entire preProcess function from controllers\ContactController and just hack away at it until it does what you want....