Wednesday, September 9, 2015

prestashop add custom field in contact us form


In this tutorial, i will show you how to add custom field in our prestashop1.6 default contact form, first lets open \default-bootstrap\contact-form.tpl and add this codes
<p class="form-group">
 <label for="email">{l s='Name'}</label>
 <input class="form-control grey" type="text" id="name" name="name" value="{if isset($name)}{$name|escape:'htmlall':'UTF-8'|stripslashes}{/if}" />      
</p>
<p class="form-group">
 <label for="email">{l s='Contact Number'}</label>
 <input class="form-control grey" type="text" id="number" name="number" value="{if isset($number)}{$number|escape:'htmlall':'UTF-8'|stripslashes}{/if}" />
</p>

You may add this in any arrangement you like but make sure that is is within the form

 Next Step: lets open our Controller for our contact form. \controllers\front\ContactController.php
Find this line
$message = Tools::getValue('message'); // Html entities is not usefull, iscleanHtml check there is no bad html tags.
add this after
$name = Tools::getValue('name');
$number = Tools::getValue('number');
And then we must pass this to the mail template, find this line of codes
if (!count($this->errors))
   {
    $var_list = array(
        '{order_name}' => '-',
        '{attached_file}' => '-',
        '{message}' => Tools::nl2br(stripslashes($message)),
        '{email}' =>  $from,
        '{product_name}' => '',       
       ); 
It should look like the codes below, we added 2 new variable
if (!count($this->errors))
   {
    $var_list = array(
        '{order_name}' => '-',
        '{attached_file}' => '-',
        '{message}' => Tools::nl2br(stripslashes($message)),
        '{email}' =>  $from,
        '{product_name}' => '',
        '{name}' => Tools::nl2br(stripslashes($name)),
        '{number}' => Tools::nl2br(stripslashes($number)),
       ); 
We are going to give back the variables that we have submitted in our controller to our view so that when a user encounter an mistake, his/her input will remain. find this code
public function initContent()
{
 parent::initContent();

 $this->assignOrderList();

 $email = Tools::safeOutput(Tools::getValue('from',
 ((isset($this->context->cookie) && isset($this->context->cookie->email) && Validate::isEmail($this->context->cookie->email)) ? $this->context->cookie->email : '')));
 $this->context->smarty->assign(array(
  'errors' => $this->errors,
  'email' => $email,
  'fileupload' => Configuration::get('PS_CUSTOMER_SERVICE_FILE_UPLOAD'),
  'max_upload_size' => (int)Tools::getMaxUploadSize()
 ));

 if (($id_customer_thread = (int)Tools::getValue('id_customer_thread')) && $token = Tools::getValue('token'))
 {
  $customer_thread = Db::getInstance()->getRow('
   SELECT cm.*
   FROM '._DB_PREFIX_.'customer_thread cm
   WHERE cm.id_customer_thread = '.(int)$id_customer_thread.'
   AND cm.id_shop = '.(int)$this->context->shop->id.'
   AND token = \''.pSQL($token).'\'
  ');

  $order = new Order((int)$customer_thread['id_order']);
  if (Validate::isLoadedObject($order))
   $customer_thread['reference'] = $order->getUniqReference();
  $this->context->smarty->assign('customerThread', $customer_thread);
 }

 $this->context->smarty->assign(array(
  'contacts' => Contact::getContacts($this->context->language->id),
  'message' => html_entity_decode(Tools::getValue('message')),
 ));

 $this->setTemplate(_PS_THEME_DIR_.'contact-form.tpl');
}
change $this->context->smarty->assign(array( take note that we added our 2 variables
$this->context->smarty->assign(array(
 'contacts' => Contact::getContacts($this->context->language->id),
 'message' => html_entity_decode(Tools::getValue('message')),
 'name' => html_entity_decode(Tools::getValue('name')),
 'number' => html_entity_decode(Tools::getValue('number')),
));

At this point everything is already working but first we need to indicate in our email template that we must add the 2 new variables, Open \mails\en\contact.html 
<span style="color:#333"><strong>Customer Name:</strong></span> {name}<br /><br />
<span style="color:#333"><strong>Customer Number:</strong></span> {number}<br /><br />
And thats it it should work!!