Jump to content

[FREE TUTORIAL] Adding new fields to the contact form


NemoPS

Recommended Posts

  • 4 weeks later...

Hi Nemo1,

 

I have followed the steps, but have a problem. I am adding 2 extra fields, some have called them extrafield1 and extrafield2, and added them both to each step. But the values are not being passed to the database. I have pasted my file changes below, does anything look wrong to you?

 

themes/default/contact_form.tpl

 

<p class="text">

<label for="extrafield1">{l s='Your name'}</label>

{if isset($customerThread.extrafield1)}

<input type="text" id="extrafield1" name="extrafield1" value="{$customerThread.extrafield1|escape:'htmlall':'UTF-8'}" readonly="readonly" />

{else}

<input type="text" id="extrafield1" name="extrafield1" value="" />

{/if}

</p>

 

then

 

<p class="text">

<label for="extrafield2">{l s='Message subject'}</label>

{if isset($customerThread.extrafield2)}

<input type="text" id="extrafield2" name="extrafield2" value="{$customerThread.extrafield2|escape:'htmlall':'UTF-8'}" readonly="readonly" />

{else}

<input type="text" id="extrafield2" name="extrafield2" value="" />

{/if}

</p>

 

override/classes/CustomerThread.php

 

<?php

 

class CustomerThread extends CustomerThreadCore

{

public $extrafield;

 

public static $definition = array(

'table' => 'customer_thread',

'primary' => 'id_customer_thread',

'fields' => array(

'id_lang' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),

'id_contact' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),

'id_shop' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),

'id_customer' =>array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),

'id_order' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),

'id_product' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),

'email' => array('type' => self::TYPE_STRING, 'validate' => 'isEmail', 'size' => 254),

'token' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true),

'status' => array('type' => self::TYPE_STRING),

'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),

'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),

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

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

),

);

}

 

override/controllers/front/ContactConroller.php

 

if ($contact->customer_service)

{

if ((int)$id_customer_thread)

{

$ct = new CustomerThread($id_customer_thread);

$ct->status = 'open';

$ct->id_lang = (int)$this->context->language->id;

$ct->id_contact = (int)($id_contact);

if ($id_order = (int)Tools::getValue('id_order'))

$ct->id_order = $id_order;

if ($id_product = (int)Tools::getValue('id_product'))

$ct->id_product = $id_product;

$ct->extrafield1 = Tools::getValue('extrafield1');

$ct->extrafield2 = Tools::getValue('extrafield2');

$ct->update();

}

else

{

$ct = new CustomerThread();

if (isset($customer->id))

$ct->id_customer = (int)($customer->id);

$ct->id_shop = (int)$this->context->shop->id;

if ($id_order = (int)Tools::getValue('id_order'))

$ct->id_order = $id_order;

if ($id_product = (int)Tools::getValue('id_product'))

$ct->id_product = $id_product;

$ct->id_contact = (int)($id_contact);

$ct->id_lang = (int)$this->context->language->id;

$ct->email = $from;

$ct->status = 'open';

$ct->token = Tools::passwdGen(12);

$ct->extrafield1 = Tools::getValue('extrafield1');

$ct->extrafield2 = Tools::getValue('extrafield2');

$ct->add();

}

 

if ($ct->id)

 

 

then

 

if (!count($this->errors))

{

$var_list = array(

'{order_name}' => '-',

'{attached_file}' => '-',

'{message}' => Tools::nl2br(stripslashes($message)),

'{email}' => $from,

'{extrafield1}' => (isset($ct) && $ct->extrafield1) ? $ct->extrafield1 : '',

'{extrafield2}' => (isset($ct) && $ct->extrafield2) ? $ct->extrafield2 : ''

);

 

Please see the attachment for the ps_customer_thread table settings

post-27482-0-91433400-1377286885_thumb.jpg

Edited by duratex (see edit history)
Link to comment
Share on other sites

I think I've just spotted my error...

 

at the top of override/classes/CustomerThread.php, I have

 

<?php

 

class CustomerThread extends CustomerThreadCore

{

public $extrafield;

 

 

I forgot to change public $extrafield; to public $extrafield#

 

how would I arrange this? like

 

 

<?php

 

class CustomerThread extends CustomerThreadCore

{

public $extrafield1;

public $extrafield2;

 

public static $definition = array(

'table' => 'customer_thread',

'primary' => 'id_customer_thread',

'fields' => array(

'id_lang' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),

'id_contact' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),

'id_shop' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),

'id_customer' =>array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),

'id_order' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),

'id_product' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),

'email' => array('type' => self::TYPE_STRING, 'validate' => 'isEmail', 'size' => 254),

'token' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true),

'status' => array('type' => self::TYPE_STRING),

'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),

'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),

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

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

),

);

}

 

Thanks for any advice!

Edited by mowax (see edit history)
Link to comment
Share on other sites

Hi Nemo1,

 

I tried arranging them as per my previous post, but I'm obviously in over my head as it doesn't work. Do you have an idea how to modify override/classes/CustomerThread.php to add a second extrafield?

 

Thanks for any help

Mo

Link to comment
Share on other sites

When I try adding both extrafield1 and 2 to CustomerThread.php as follows, the contact-us page doesn't load at all.

 

<?php

 

class CustomerThread extends CustomerThreadCore

{

public $extrafield1;

public $extrafield2;

Link to comment
Share on other sites

Nice tutorial!

 

If you want to keep the core definitions you could also write the model override something like this:

 

class CustomerThread extends CustomerThreadCore
{
 public $extrafield;
 public function __construct($id = null, $id_lang = null, $id_shop = null){
   self::$definition['fields']['extrafield'] = array(
     'type' => self::TYPE_STRING,
     'validate' => 'isGenericName',
   );
   parent::__construct($id, $id_lang, $id_shop);
 }
}

 

(On a sidenote, most of PrestaShop's controllers are in a dire need of better functional cohesion, as overriding huge methods like postProcess in this case prevent core updates on those methods from taking effect)

Link to comment
Share on other sites

  • 2 weeks later...
  • 4 weeks later...
  • 1 month later...
  • 4 weeks later...

Tutorial is great but i have problems with it . 

override/classes/CustomerThread.php ==>

<?php
class CustomerThread extends CustomerThreadCore
{
  public $id_client;
  public $id_adress;
  public $id_comp;
 
    public static $definition = array(
        'table' => 'customer_thread',
        'primary' => 'id_customer_thread',
        'fields' => array(
            'id_lang' =>     array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
            'id_contact' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
            'id_shop' =>     array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'id_customer' =>array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'id_order' =>    array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'id_product' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'id_client' =>  array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
            'id_adress' =>  array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),	                'id_comp' =>  array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
            'status' =>  array('type' => self::TYPE_STRING),
            'date_add' =>    array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
            'date_upd' =>    array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
            
        ),
    );
 
 
}

prestashop/controllers/ContactController.php

if ($contact->customer_service)
{
if ((int)$id_customer_thread)
{
$ct = new CustomerThread($id_customer_thread);
$ct->status = 'open';
$ct->id_lang = (int)$this->context->language->id;
$ct->id_contact = (int)($id_contact);
if ($id_order = (int)Tools::getValue('id_order'))
$ct->id_order = $id_order;
if ($id_product = (int)Tools::getValue('id_product'))
$ct->id_product = $id_product;
$ct->id_client = Tools::getValue('id_client'); 
$ct->id_adress = Tools::getValue('id_adress'); 
$ct->id_comp = Tools::getValue('id_comp');
$ct->update();
}
else
{
$ct = new CustomerThread();
if (isset($customer->id))
$ct->id_customer = (int)($customer->id);
$ct->id_shop = (int)$this->context->shop->id;
if ($id_order = (int)Tools::getValue('id_order'))
$ct->id_order = $id_order;
if ($id_product = (int)Tools::getValue('id_product'))
$ct->id_product = $id_product;
$ct->id_contact = (int)($id_contact);
$ct->id_lang = (int)$this->context->language->id;
$ct->email = $from;
$ct->status = 'open';
$ct->token = Tools::passwdGen(12);
$ct->id_client = (string)Tools::getValue('id_client'); 
$ct->id_adress = (string)Tools::getValue('id_adress'); 
$ct->id_comp = (string)Tools::getValue('id_comp');
$ct->add();
}

					if ($ct->id)
                    {
                        $cm = new CustomerMessage();
                        $cm->id_customer_thread = $ct->id;
                        $cm->message = Tools::htmlentitiesUTF8($message);
                        if (isset($filename) && rename($_FILES['fileUpload']['tmp_name'], _PS_MODULE_DIR_.'../upload/'.$filename))
                            $cm->file_name = $filename;
                        $cm->ip_address = ip2long($_SERVER['REMOTE_ADDR']);
                        $cm->user_agent = $_SERVER['HTTP_USER_AGENT'];
                        if (!$cm->add())
                            $this->errors[] = Tools::displayError('An error occurred while sending the message.');
                    }
                    else
                        $this->errors[] = Tools::displayError('An error occurred while sending the message.');
                }
 
                if (!count($this->errors))
                {
                    $var_list = array(
                                    '{order_name}' => '-',
                                    '{attached_file}' => '-',
                                    '{message}' => Tools::nl2br(stripslashes($message)),
                                    '{email}' =>  $from,	
									'{id_client}' =>  (isset($ct) && $ct->id_client) ? $ct->id_client : '',
									'{id_adress}' =>  (isset($ct) && $ct->id_adress) ? $ct->id_adress : '',
									'{id_comp}' =>  (isset($ct) && $ct->id_comp) ? $ct->id_comp : '',
				);

ubwvg5.jpg

 

And when I want to send, I get an error. (e-mail is sent, but the error is displayed)g8vgl7.jpg

Edited by pbpo (see edit history)
Link to comment
Share on other sites

  • 7 months later...
  • 1 month later...

Hello Nemo1!

 

Thank you for your good tutorial and advaices always!

I tried to customize the contact form and it works fine!

 

If possible I want know how to make a input area "required" and show the error if it is empty.

(I use version 1.6.0.9)

Thank you in advance!

Hi, Tatamimi
 
I wanted too use this tutorial in Prestashop 1.6.0.9 but i have problem with mails.
 
firstly I just created 2 extra fields , they are in:
 
- contact.tpl
- CustomerThread.php
- ContactController.php
- contact-form.tpl
- In mails: 
    > contact.html
    > contact-form.html
 
Just as it is in this tutorial 
 
E-mail was sended correctly but when I see mail in  my mail-program, fields are empty ( didn't downloaded fields content )
 
Could you send me yours files ? 
I want them to see and create my own
 
Link to comment
Share on other sites

Hi pbpo!

Did you add the database value ?

Sorry I don't use the field of downloaded files in my case( I delated these contents from the mail and form ) so I don't know it works well at that point...

 

In my case,  {products} (the detail of order in the mail) didn't work at first.
It was because of the change or controll files in the version 1.6.0.9. (I use override file made before 1.6.0.6)
I copied the controlle files from 1.6.0.9 default template and added my code for add the contact form contents and upload again, after it works.

Link to comment
Share on other sites

Yes I add the database value. 
 
When mail is sent. Fields are empty.
svdyiu.jpg
 
My files:

contact-form.tpl

<p class="form-group">
<label for="id_imie">{l s='id_imie'}</label>
{if isset($customerThread.id_imie)}
        <input class="form-control grey" type="text" id="id_imie" name="id_imie" value="{$customerThread.id_imie|escape:'htmlall':'UTF-8'}" readonly="readonly" />
     {else}
        <input class="form-control grey validate" type="text" id="id_imie" name="id_imie" value="" />
     {/if}    
    <p>(example: Jan)</p>
</p>

ContactController.php

if ($contact->customer_service)
{
if ((int)$id_customer_thread)
{
$ct = new CustomerThread($id_customer_thread);
$ct->status = 'open';
$ct->id_lang = (int)$this->context->language->id;
$ct->id_contact = (int)($id_contact);
$ct->id_order = (int)$id_order;
if ($id_product = (int)Tools::getValue('id_product'))
$ct->id_product = $id_product; 
$ct->id_imie = Tools::getValue('id_imie'); 
$ct->id_nazwisko = Tools::getValue('id_nazwisko'); 
$ct->id_organ = Tools::getValue('id_organ');
$ct->id_adres = Tools::getValue('id_adres');
$ct->id_poczta = Tools::getValue('id_poczta');
$ct->id_miasto = Tools::getValue('id_miasto');
$ct->id_telefon = Tools::getValue('id_telefon');
$ct->update();
}
else
{
$ct = new CustomerThread();
if (isset($customer->id))
$ct->id_customer = (int)($customer->id);
$ct->id_shop = (int)$this->context->shop->id;
$ct->id_order = (int)$id_order;
if ($id_product = (int)Tools::getValue('id_product'))
$ct->id_product = $id_product;
$ct->id_contact = (int)($id_contact);
$ct->id_lang = (int)$this->context->language->id;
$ct->email = $from;
$ct->status = 'open';
$ct->token = Tools::passwdGen(12);
$ct->id_imie = Tools::getValue('id_imie'); 
$ct->id_nazwisko = Tools::getValue('id_nazwisko'); 
$ct->id_organ = Tools::getValue('id_organ');
$ct->id_adres = Tools::getValue('id_adres');
$ct->id_poczta = Tools::getValue('id_poczta');
$ct->id_miasto = Tools::getValue('id_miasto');
$ct->id_telefon = Tools::getValue('id_telefon');
$ct->add();
}


if (!count($this->errors))
{
$var_list = array(
'{order_name}' => '-',
'{attached_file}' => '-',
'{message}' => Tools::nl2br(stripslashes($message)),
'{email}' =>  $from,
'{product_name}' => '', 
'{id_imie}' =>  (isset($ct) && $ct->id_imie) ? $ct->id_imie : '',
'{id_nazwisko}' =>  (isset($ct) && $ct->id_nazwisko) ? $ct->id_nazwisko : '',
'{id_organ}' =>  (isset($ct) && $ct->id_organ) ? $ct->id_organ : '',
'{id_adres}' =>  (isset($ct) && $ct->id_adres) ? $ct->id_adres : '',
'{id_poczta}' =>  (isset($ct) && $ct->id_poczta) ? $ct->id_poczta : '',
'{id_miasto}' =>  (isset($ct) && $ct->id_miasto) ? $ct->id_miasto : '',
'{id_telefon}' =>  (isset($ct) && $ct->id_telefon) ? $ct->id_telefon : '',
);

 

CustomerThread.php

<?php


class CustomerThread extends CustomerThreadCore
{
    public $id_imie; 
    public $id_nazwisko; 
    public $id_organ;
    public $id_adres;
    public $id_poczta;
    public $id_miasto;
    public $id_telefon;


    public static $definition = array(
        'table' => 'customer_thread',
        'primary' => 'id_customer_thread',
        'fields' => array(
'id_lang' =>  array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_contact' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_shop' =>  array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_customer' =>array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_order' =>  array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_product' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'email' =>  array('type' => self::TYPE_STRING, 'validate' => 'isEmail', 'size' => 254), 
            'id_imie' =>  array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 50, 'required' => true),
            'id_nazwisko' =>  array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 50, 'required' => true),
            'id_organ' =>  array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255, 'required' => true),
            'id_adres' =>  array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255, 'required' => true),
            'id_poczta' =>  array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 15, 'required' => true),
            'id_miasto' =>  array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 30, 'required' => true),
            'id_telefon' =>  array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 15, 'required' => true),
'token' =>  array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true),
'status' =>  array('type' => self::TYPE_STRING),
'date_add' =>  array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
'date_upd' =>  array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
        ),
    );




}

and message.tpl

{if !$email}
            <dl class="dl-horizontal"><dt>{l s='id_imie:'}</dt>
<dd>{$message.id_imie}</dd>
</dl> 
<dl class="dl-horizontal">
<dt>{l s='id_nazwisko:'}</dt>
<dd>{$message.id_nazwisko}</dd>
</dl> 
<dl class="dl-horizontal">
<dt>{l s='id_organ:'}</dt>
<dd>{$message.id_organ}</dd>
</dl> 
<dl class="dl-horizontal">
<dt>{l s='id_adres:'}</dt>
<dd>{$message.id_adres}</dd>
</dl> 
<dl class="dl-horizontal">
<dt>{l s='id_poczta:'}</dt>
<dd>{$message.id_poczta}</dd>
</dl> 
<dl class="dl-horizontal">
<dt>{l s='id_miasto:'}</dt>
<dd>{$message.id_miasto}</dd>
</dl> 
<dl class="dl-horizontal">
<dt>{l s='id_telefon:'}</dt>
<dd>{$message.id_telefon}</dd>
</dl> 
<dl class="dl-horizontal">
<dt>{l s='Thread ID:'}</dt>
<dd>{$message.id_customer_thread}</dd>
</dl>
<dl class="dl-horizontal">
<dt>{l s='Message ID:'}</dt>
<dd>{$message.id_customer_message}</dd>
</dl>
{/if}

And in phpmyadmin :kgwhw9.jpg

 

Edited by pbpo (see edit history)
Link to comment
Share on other sites

That so let me quote:
http://nemops.com/adding-new-fields-to-prestashop-contact-form/#.VCJV-_l_t5P

"
Now, as a really last step, we will add the field to the back office as well. Again, following the maintainability principle, we will not directly modify the core files, but use overrides.

Go to your admin folder, then themes\default\template\controllers\customer_threads, and copy message.tpl.
"

Link to comment
Share on other sites

  • 4 weeks later...
  • 2 months later...
  • 3 months later...

Hi can someone please help me with this part i am not sure how to do this or where ??? 

 

The field is there, but our database doesn’t know. Let’s tell it! Login to the database using your favorite tool (my choice is always phpMyAdmin).Locate the ps_customer_thread table (as always, your prefix might be other than ps_).

Add a new TEXT column with no limitations. Again, if you chose something other than a text input, like a radio or checkbox, you might want to use TINYINT instead, to add only 1 or 0.

Link to comment
Share on other sites

  • 2 months later...

Very nice module! I have just one question from one of my clients. How can I prove to him that you guys are not logging, tracking all the messages? There are server side calls from JS in this code so all the user information should be passive of being logged.

Edited by pbraconnot (see edit history)
Link to comment
Share on other sites

  • 1 month later...

Hi Nemo,

Thank you for your tutorial

I managed to put half the name and phone contact-form for the 1.4.11 version of PrestaShop

testing there send two messages:


- The first message via email to the seller, there has name, phone, email and message displays but no id_order or id_product


- 2nd message via email to the customer advising that his message has been sent, there has id.order, id.product, email and messages but not names or phone


Is it possible to bring together in one so the seller and customer can receive the same message?

eg:

Name: Nemo

phone nuber : 06xx

mail: [email protected]

id_order: xxl12

id_product: bronzage

Message: received the purchased tanning product


Thank you in advance

Edited by Khundo (see edit history)
Link to comment
Share on other sites

  • 3 months later...

Hello. I'm using 1.6.0.11 version, but in var_dump() say me "Undefined index "telefono". ¿There is some issue with this version and overrides?

 

PD: I always edit my table ps_customer_thread

 

override/clasess/CustomerThread.php

<?php
 
class CustomerThread extends CustomerThreadCore
{
    public $telefono;
 
    public static $definition = array(
        'table' => 'customer_thread',
        'primary' => 'id_customer_thread',
        'fields' => array(
            'id_lang' =>     array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
            'id_contact' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
            'id_shop' =>     array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'id_customer' =>array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'id_order' =>    array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'id_product' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'email' =>       array('type' => self::TYPE_STRING, 'validate' => 'isEmail', 'size' => 254),
            'token' =>       array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true),
            'status' =>  array('type' => self::TYPE_STRING),
            'date_add' =>    array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
            'date_upd' =>    array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
            'telefono' =>  array('type' => self::TYPE_STRING, 'validate' => 'isPhoneNumber', 'size' => 15, 'required' => true),
        ),
    ); 
}

(in my theme folder) contact-form.tpl

<p class="form-group">
    <label for="telefono">{l s='Teléfono'}<sup>*</sup></label>
        {if isset($customerThread.telefono)}
            <input class="form-control" type="text" id="telefono" name="telefono" value="{$customerThread.telefono|escape:'htmlall':'UTF-8'}" readonly="readonly" />
        {else}
            <input class="form-control validate" type="text" id="telefono" name="telefono" data-validate="isPhoneNumber" value="{$smarty.post.telefono|escape:'htmlall':'UTF-8'}" />
        {/if}
</p>

override/controllers/front/ContactController.php

<?php
 
class ContactController extends ContactControllerCore
{
    public function postProcess()
    {
        if (Tools::isSubmit('submitMessage'))
        {
            $fileAttachment = null;
            if (isset($_FILES['fileUpload']['name']) && !empty($_FILES['fileUpload']['name']) && !empty($_FILES['fileUpload']['tmp_name']))
            {
                $extension = array('.txt', '.rtf', '.doc', '.docx', '.pdf', '.zip', '.png', '.jpeg', '.gif', '.jpg');
                $filename = uniqid().substr($_FILES['fileUpload']['name'], -5);
                $fileAttachment['content'] = file_get_contents($_FILES['fileUpload']['tmp_name']);
                $fileAttachment['name'] = $_FILES['fileUpload']['name'];
                $fileAttachment['mime'] = $_FILES['fileUpload']['type'];
            }
            $message = Tools::getValue('message'); // Html entities is not usefull, iscleanHtml check there is no bad html tags.
            if (!($from = trim(Tools::getValue('from'))) || !Validate::isEmail($from))
                $this->errors[] = Tools::displayError('Invalid email address.');
            else if (!$message)
                $this->errors[] = Tools::displayError('The message cannot be blank.');
            else if (!Validate::isCleanHtml($message))
                $this->errors[] = Tools::displayError('Invalid message');
            else if (!($id_contact = (int)(Tools::getValue('id_contact'))) || !(Validate::isLoadedObject($contact = new Contact($id_contact, $this->context->language->id))))
                $this->errors[] = Tools::displayError('Please select a subject from the list provided. ');
            else if (!empty($_FILES['fileUpload']['name']) && $_FILES['fileUpload']['error'] != 0)
                $this->errors[] = Tools::displayError('An error occurred during the file-upload process.');
            else if (!empty($_FILES['fileUpload']['name']) && !in_array(substr($_FILES['fileUpload']['name'], -4), $extension) && !in_array(substr($_FILES['fileUpload']['name'], -5), $extension))
                $this->errors[] = Tools::displayError('Bad file extension');
            else
            {
                $customer = $this->context->customer;
                if (!$customer->id)
                    $customer->getByEmail($from);
 
                $contact = new Contact($id_contact, $this->context->language->id);
 
                if (!((
                        ($id_customer_thread = (int)Tools::getValue('id_customer_thread'))
                        && (int)Db::getInstance()->getValue('
                        SELECT cm.id_customer_thread 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(Tools::getValue('token')).'\'')
                    ) || (
                        $id_customer_thread = CustomerThread::getIdCustomerThreadByEmailAndIdOrder($from, (int)Tools::getValue('id_order'))
                    )))
                {
                    $fields = Db::getInstance()->executeS('
                    SELECT cm.id_customer_thread, cm.id_contact, cm.id_customer, cm.id_order, cm.id_product, cm.email
                    FROM '._DB_PREFIX_.'customer_thread cm
                    WHERE email = \''.pSQL($from).'\' AND cm.id_shop = '.(int)$this->context->shop->id.' AND ('.
                        ($customer->id ? 'id_customer = '.(int)($customer->id).' OR ' : '').'
                        id_order = '.(int)(Tools::getValue('id_order')).')');
                    $score = 0;
                    foreach ($fields as $key => $row)
                    {
                        $tmp = 0;
                        if ((int)$row['id_customer'] && $row['id_customer'] != $customer->id && $row['email'] != $from)
                            continue;
                        if ($row['id_order'] != 0 && Tools::getValue('id_order') != $row['id_order'])
                            continue;
                        if ($row['email'] == $from)
                            $tmp += 4;
                        if ($row['id_contact'] == $id_contact)
                            $tmp++;
                        if (Tools::getValue('id_product') != 0 && $row['id_product'] == Tools::getValue('id_product'))
                            $tmp += 2;
                        if ($tmp >= 5 && $tmp >= $score)
                        {
                            $score = $tmp;
                            $id_customer_thread = $row['id_customer_thread'];
                        }
                    }
                }
                $old_message = Db::getInstance()->getValue('
                    SELECT cm.message FROM '._DB_PREFIX_.'customer_message cm
                    LEFT JOIN '._DB_PREFIX_.'customer_thread cc on (cm.id_customer_thread = cc.id_customer_thread)
                    WHERE cc.id_customer_thread = '.(int)($id_customer_thread).' AND cc.id_shop = '.(int)$this->context->shop->id.'
                    ORDER BY cm.date_add DESC');
                if ($old_message == $message)
                {
                    $this->context->smarty->assign('alreadySent', 1);
                    $contact->email = '';
                    $contact->customer_service = 0;
                }
 
                if ($contact->customer_service)
                {
                    if ((int)$id_customer_thread)
                    {
                        $ct = new CustomerThread($id_customer_thread);
                        $ct->status = 'open';
                        $ct->id_lang = (int)$this->context->language->id;
                        $ct->id_contact = (int)($id_contact);
                        if ($id_order = (int)Tools::getValue('id_order'))
                            $ct->id_order = $id_order;
                        if ($id_product = (int)Tools::getValue('id_product'))
                            $ct->id_product = $id_product;
                        $ct->telefono = Tools::getValue('telefono');
                        $ct->update();
                    }
                    else
                    {
                        $ct = new CustomerThread();
                        if (isset($customer->id))
                            $ct->id_customer = (int)($customer->id);
                        $ct->id_shop = (int)$this->context->shop->id;
                        if ($id_order = (int)Tools::getValue('id_order'))
                            $ct->id_order = $id_order;
                        if ($id_product = (int)Tools::getValue('id_product'))
                            $ct->id_product = $id_product;
                        $ct->id_contact = (int)($id_contact);
                        $ct->id_lang = (int)$this->context->language->id;
                        $ct->email = $from;
                        $ct->status = 'open';
                        $ct->token = Tools::passwdGen(12);
                        $ct->telefono = Tools::getValue('telefono');
                        $ct->add();
                    }
 
                    if ($ct->id)
                    {
                        $cm = new CustomerMessage();
                        $cm->id_customer_thread = $ct->id;
                        $cm->message = Tools::htmlentitiesUTF8($message);
                        if (isset($filename) && rename($_FILES['fileUpload']['tmp_name'], _PS_MODULE_DIR_.'../upload/'.$filename))
                            $cm->file_name = $filename;
                        $cm->ip_address = ip2long($_SERVER['REMOTE_ADDR']);
                        $cm->user_agent = $_SERVER['HTTP_USER_AGENT'];
                        if (!$cm->add())
                            $this->errors[] = Tools::displayError('An error occurred while sending the message.');
                    }
                    else
                        $this->errors[] = Tools::displayError('An error occurred while sending the message.');
                }
 
                if (!count($this->errors))
                {
                    $var_list = array(
                        '{order_name}' => '-',
                        '{attached_file}' => '-',
                        '{message}' => Tools::nl2br(stripslashes($message)),
                        '{email}' =>  $from,
                        '{telefono}' =>  (isset($ct) && $ct->telefono) ? $ct->telefono : ''
                    );
 
                    if (isset($filename))
                        $var_list['{attached_file}'] = $_FILES['fileUpload']['name'];
 
                    $id_order = (int)Tools::getValue('id_order');
                     
                    if (isset($ct) && Validate::isLoadedObject($ct))
                    {
                        if ($ct->id_order)
                            $id_order = $ct->id_order;
                        $subject = sprintf(Mail::l('Your message has been correctly sent #ct%1$s #tc%2$s'), $ct->id, $ct->token);
                    }
                    else
                        $subject = Mail::l('Your message has been correctly sent');
 
                    if ($id_order)
                    {
                        $order = new Order((int)$id_order);
                        $var_list['{order_name}'] = $order->getUniqReference();
                        $var_list['{id_order}'] = $id_order;
                    }
                     
                    if (empty($contact->email))
                        Mail::Send($this->context->language->id, 'contact_form', $subject, $var_list, $from, null, null, null, $fileAttachment);
                    else
                    {                   
                        if (!Mail::Send($this->context->language->id, 'contact', Mail::l('Message from contact form').' [no_sync]',
                            $var_list, $contact->email, $contact->name, $from, ($customer->id ? $customer->firstname.' '.$customer->lastname : ''),
                                    $fileAttachment) ||
                                !Mail::Send($this->context->language->id, 'contact_form', $subject, $var_list, $from, null, $contact->email, $contact->name, $fileAttachment))
                                    $this->errors[] = Tools::displayError('An error occurred while sending the message.');
                    }
                }
                 
                if (count($this->errors) > 1)
                    array_unique($this->errors);
                else
                    $this->context->smarty->assign('confirmation', 1);
            }
        }
    }
 
}

¿Can you help me? Thanks.

Edited by rramos-acuabit (see edit history)
Link to comment
Share on other sites

  • 4 weeks later...

Does this guide apply to presta 1.6?

 

In 1.6.0.9 tested - work perfect

 

In 1.6.0.11 not tested but I will try to do this.

 

 

EDIT:

 

In 1.6.0.11 not work...

 

Have to wait for check it by Nemo.

 

 

In lastest version Prestashop messages are sending correct, but in received mails new fields are empty.

 

No errors in debug mode

Edited by pbpo (see edit history)
Link to comment
Share on other sites

I have presta 1.6.1.1 And field in database is not populated, it was an upgrade from 1.5.4 presta (it worked) I re-did the entire procedure and its not working.

 

 

EDIT:

 

I have managed to make it submit info in the database, but the problem now is that it won't show in backend, i made the override and even edited core file message.tpl there are no changes, even when you empty the file it is still the same... I have disabled cache and it is on force compilation. I can't seem to find what's causing this?

 

 

 

Regards

Edited by magix01 (see edit history)
Link to comment
Share on other sites

  • 3 weeks later...

OK, so I managed to get this working great on 1.6.1.1 (overriding controllers/admin/templates/customer_threads/helpers/view/views.tpl instead of message.tpl in the tutorual - an I had to copy in the otehr files in the views folder too for it to work).

 

Anyway, then I went to test out my newly installed PayPal Europe module (the free, official one) and found that there was a conflict.  The PayPal payment would go through, order would go through back office but I woudl get a blank page on returning to the shop after paying instead if seeing the confirmation page.  The url was ... modules\paypal\express_checkout\payment.php (although not usin express checkout??) and the error included:

 

Fatal error: Uncaught exception 'PrestaShopException' with message 'Property CustomerThread->extrafield is empty' in ......\classes\ObjectModel.php:909nStack trace:n#0

 

 

I have had to disable the extra fields to get PayPal to work.  What is causing the issue as PayPal should have nothing to do with the Customer Thread? How can I get the two working together?

 

There are actually two extra fields on the contact page, but the extrafield flagged up in the error is a required field

Edited by babyewok (see edit history)
Link to comment
Share on other sites

  • 2 months later...
  • 1 year later...

Hey guys,

 

First off, great tutorial! Thanks NemoPS!!

 

But I see this was written quite some time ago, has someone not gone and developed a module or something that can make editing the contact form easier since then?

 

And I dont know if something is wrong with my contact form, but it's really strange to me that the customer cannot type in his name in the form or that the form does not send the customer name inside the email. How is someone supposed to know who they are replying to if all the form is supplying is the email address and order number? It just seem like something really basic which should be included by default.

 

Am I missing something?

Edited by FanieLumia (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 1 year later...

Hi,

I want to add a mandatory checkbox in Contact form to throw an error message when the checkbox is not checked , I followed the steps from tutorial, but the error doesn't appear, something I did wrong.

Please help,

 

This is the code inserted in contact-form.tpl 
 

<h3 class="page-subheading"> Confidențialitate date client</h3><div style="width:21px; float:left;"><div class="required checkbox">
            <div class="checker" id="uniform-customer_privacy">
            <span class="checkbox">
            <label for="ps_customer_checkbox">{l s=''}</label>
    {if isset($customerThread.ps_customer_checkbox)}
        <input type="checkbox" id="ps_customer_checkbox" name="ps_customer_checkbox" value="{$customerThread.ps_customer_checkbox|escape:'htmlall':'UTF-8'}" readonly="readonly" />
    {else}
        <input type="checkbox" value="1" id="ps_customer_checkbox" name="ps_customer_checkbox" autocomplete="off">
    {/if}
            </span>
            </div></div></div><div style="width: 92%; float: left; margin-top: 8px;"> <label for="customer_priv" style="font-weight: normal;"><!--? xml version="1.0" encoding="UTF-8" ?--><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> Datele personale oferite de dumneavoastră sunt confidentiale *</label></div>

The CustomerTheard,php:

<?php
 
class CustomerThread extends CustomerThreadCore
{
    public $ps_customer_checkbox;
 
    public static $definition = array(
        'table' => 'customer_thread',
        'primary' => 'id_customer_thread',
        'fields' => array(
            'id_lang' =>     array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
            'id_contact' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
            'id_shop' =>     array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'id_customer' =>array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'id_order' =>    array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'id_product' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'email' =>       array('type' => self::TYPE_STRING, 'validate' => 'isEmail', 'size' => 254),
            'token' =>       array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true),
            'status' =>  array('type' => self::TYPE_STRING),
            'date_add' =>    array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
            'date_upd' =>    array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
            'ps_customer_checkbox' =>  array('type' => self::TYPE_BOOL, 'validate' =>'isBool', 'required' => true),
        ),
    );
 
 
}

The code from ContactController from  override/controllers/front/ContactController.php

  if ($contact->customer_service) {
                    if ((int)$id_customer_thread) {
                        $ct = new CustomerThread($id_customer_thread);
                        $ct->status = 'open';
                        $ct->id_lang = (int)$this->context->language->id;
                        $ct->id_contact = (int)$id_contact;
                        $ct->id_order = (int)$id_order;
                        if ($id_product = (int)Tools::getValue('id_product')) {
                            $ct->id_product = $id_product;
                        }
						$ct->ps_customer_checkbox = (int)Tools::getValue('ps_customer_checkbox');
                        $ct->update();
                    } else {
                        $ct = new CustomerThread();
                        if (isset($customer->id)) {
                            $ct->id_customer = (int)$customer->id;
                        }
                        $ct->id_shop = (int)$this->context->shop->id;
                        $ct->id_order = (int)$id_order;
                        if ($id_product = (int)Tools::getValue('id_product')) {
                            $ct->id_product = $id_product;
                        }
                        $ct->id_contact = (int)$id_contact;
                        $ct->id_lang = (int)$this->context->language->id;
                        $ct->email = $from;
                        $ct->status = 'open';
                        $ct->token = Tools::passwdGen(12);
						$ct->ps_customer_checkbox = (int)Tools::getValue('ps_customer_checkbox')
                        $ct->add();
                    }
					if ($ct->ps_customer_checkbox = 0){
                            $this->errors[] = Tools::displayError('Arunca-mi eroare');

The db is populated when the checkbox is unchecked, but no error is thrown.

Thanks in advance,

Cristi

 

Link to comment
Share on other sites

  • 3 months later...

I wondered if the solution of nemo still works in 1.7 (tested in 1.7.3.4) and it kinda does, just the files are at different positions, called differently, I tried the following thing to add a field (and remember this solution is not update proof and the additional fields are not safed in the backoffice):

  • Update contact.html + contact.txt in the theme/mails folder:
    <span style="color:#333"><strong>Name: {contactname}</strong></span><br /><br />
  • Add the contactname line to the $var_list array in the file contactform.php located in the modules/contactform folder:
    $var_list = [
    '{order_name}' => '-',
    '{attached_file}' => '-',
    '{message}' => Tools::nl2br(stripslashes($message)),
    '{email}' =>  $from,
    '{product_name}' => '',
    '{contactname}' => Tools::nl2br(stripslashes($contactname)),
    ];
  • Add a line to the beginning of the function sendMessage(){ in the same file (contactform.php)   
    $contactname = trim(Tools::getValue('contactname'));
  • Add the following lines to the <section class="form-fields"> part in the file contactform.tpl located in the theme/modules/contactform/views/template/widget folder:
    <div class="form-group row">
    <label class="col-md-3 form-control-label">{l s='Name' d='Shop.Forms.Labels'}</label>
    <div class="col-md-6">
    <input
    class="form-control"
    name="contactname"
    type="text"
    value="{$contact.contactname}"
    placeholder="{l s='Ihr Name' d='Shop.Forms.Help'}"
    >
    </div>
    </div>

 

I know it's ugly, but hey, it's a start :)

  • Like 1
Link to comment
Share on other sites

  • 10 months later...
On 9/10/2018 at 3:11 PM, christian_at_joseph said:

I wondered if the solution of nemo still works in 1.7 (tested in 1.7.3.4) and it kinda does, just the files are at different positions, called differently, I tried the following thing to add a field (and remember this solution is not update proof and the additional fields are not safed in the backoffice):

  • Update contact.html + contact.txt in the theme/mails folder:

    <span style="color:#333"><strong>Name: {contactname}</strong></span><br /><br />
  • Add the contactname line to the $var_list array in the file contactform.php located in the modules/contactform folder:

    $var_list = [
    '{order_name}' => '-',
    '{attached_file}' => '-',
    '{message}' => Tools::nl2br(stripslashes($message)),
    '{email}' =>  $from,
    '{product_name}' => '',
    '{contactname}' => Tools::nl2br(stripslashes($contactname)),
    ];
  • Add a line to the beginning of the function sendMessage(){ in the same file (contactform.php)   

    $contactname = trim(Tools::getValue('contactname'));
  • Add the following lines to the <section class="form-fields"> part in the file contactform.tpl located in the theme/modules/contactform/views/template/widget folder:

    <div class="form-group row">
    <label class="col-md-3 form-control-label">{l s='Name' d='Shop.Forms.Labels'}</label>
    <div class="col-md-6">
    <input
    class="form-control"
    name="contactname"
    type="text"
    value="{$contact.contactname}"
    placeholder="{l s='Ihr Name' d='Shop.Forms.Help'}"
    >
    </div>
    </div>

 

I know it's ugly, but hey, it's a start :)

I used this workaround for PS 1.7.6 and all seems to work well except that when in debug mode, I get: Notice: Undefined index: contactname - any idea why that is?

Link to comment
Share on other sites

  • 3 months later...

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...