Top Question Answers about PrestaShop Contact Form in 2017

Posted On: Sep 14, 2018
Categories: Shopping Carts & Platforms
What you think that which part of your PrestaShop store in most important? Some will say home page, product page, checkout page etc. It’s true that all of these have a great deal in making a store successful but the final thing that should be perfect is the way through customers can contact you. If clients have problem on any of the above mentioned pages, they will definitely go to contact the shop owner so contact form should be master piece of your shop.
When you think about contact form, you may have several things in mind like
- Default contact form link on footer of PrestaShop
- PrestaShop ticket system
- Just provide a support email
Why not go more advance than that by providing a contact form on PrestaShop product pages. That will be superb idea to provide more user retention on your sites. Also there are paid module available to create product contact page but given below some free resources.
As here we are talking about problems and solution of PrestaShop, so let’s start from it.
How to add a product contact form in tab?
Question No. 1: I want to add contact form on product page of my store. How it is possible with a free solution?
Answer: You can show contact form on product page via creating a separate tab for contact page. First step should be to create a new tab using any free or paid product tab module. After creation of the tab, you need to have a link of your contact page and also you should have to decide that which email id will be used to receive emails from customers.
When you have both the things ready, just go to new tabs section (that you have created earlier) and paste the below code in it.
<form action="_____URL_TO_CONTACT_FORM_____"method="post"class="contact-form-box"enctype="multipart/form-data">
<input type="hidden"name="id_contact"value="_____ID_OF_CONTACT_____">
<fieldset>
<h3class="page-subheading">send a message</h3>
<div class="clearfix">
<div class="col-xs-12 col-md-3">
<div class="form-group selector1">
<label for="email">Email address</label>
<input class="form-control grey validate"type="text"id="email"name="from"data-validate="isEmail"value="">
</div>
<div class="submit">
<button type="submit"name="submitMessage"id="submitMessage"class="button btn btn-default button-medium"><span>Send<i class="icon-chevron-right right"></i></span></button>
</div>
</div>
<div class="col-xs-12 col-md-9">
<div class="form-group">
<label for="message">Message</label>
<textarea style="min-height:100px;"class="form-control"id="message"name="message">Question about {name}</textarea>
</div>
</div>
</div>
</fieldset>
</form>
In the first line, you have to give the url of the contact form of your page and in the 2nd line write the email ID.
Where to add the code for contact form on a CMS page?
Question No. 2: I want to create a create a contact form on a CMS page so that the users can enter their company information e.g. name, email and then I will send some piece of information to them. Where exactly I have to write the code?
Answer: if you want to add code on a CMS page, you have to go to CMScontroller.php
file located in controllers/front/ directory in init() function
. However, you need to use an editor that can accept Php code.
How to add a iframe to contact form of a CMS page?
Question No. 3: I have tried to add an iframe on contact form page but it is not accepting. How it is possible?
Answer: Go to preferences>general
and switch on this option “Allow iframes on html fields”.
How to add a contact form to a CMS page?
Question No. 4: I want to create a contact form on a CMS page. Is it possible through coding?
Answer: Yes, it is possible with editing some of the files as I mentioned below;
In CMSController.php
after
public function init()
I added
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(Tools::strtolower($_FILES['fileUpload']['name']), -4), $extension) && !in_array(substr(Tools::strtolower($_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->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->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,
);
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);
}
}
}
inside
public function setMedia()
I added
$this->addCSS(_THEME_CSS_DIR_.'contact-form.css');
$this->addJS(_THEME_JS_DIR_.'contact-form.js');
inside
public function initContent()
before
$this->setTemplate(_PS_THEME_DIR_.'cms.tpl');
I added
$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')
));
if (($id_customer_thread = (int)Tools::getValue('id_customer_thread')) && $token = Tools::getValue('token'))
{
$customerThread = 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).''
');
$this->context->smarty->assign('customerThread', $customerThread);
}
$this->context->smarty->assign(array(
'contacts' => Contact::getContacts($this->context->language->id),
'message' => html_entity_decode(Tools::getValue('message'))
));
and after
public function initContent()
I added
protected function assignOrderList()
{
if ($this->context->customer->isLogged())
{
$this->context->smarty->assign('isLogged', 1);
$products = array();
$result = Db::getInstance()->executeS('
SELECT id_order
FROM '._DB_PREFIX_.'orders
WHERE id_customer = '.(int)$this->context->customer->id.' ORDER BY date_add');
$orders = array();
foreach ($result as $row)
{
$order = new Order($row['id_order']);
$date = explode(' ', $order->date_add);
$tmp = $order->getProducts();
foreach ($tmp as $key => $val)
$products[$row['id_order']][$val['product_id']] = array('value' => $val['product_id'], 'label' => $val['product_name']);
$orders[] = array('value' => $order->id, 'label' => $order->getUniqReference().' - '.Tools::displayDate($date[0], $this->context->language->id), 'selected' => (int)Tools::getValue('id_order') == $order->id);
}
$this->context->smarty->assign('orderList', $orders);
$this->context->smarty->assign('orderedProductList', $products);
}
}
Now in cms.tpl of my theme I did
after
{$cms->content}
{if $cms->id == 25}
{if isset($confirmation)}
<p>{l s='Your message has been successfully sent to our team.'}</p>
<ul class="footer_links">
<li><a href="{$base_dir}"><img class="icon" alt="" src="{$img_dir}icon/home.gif"/></a><a href="{$base_dir}">{l s='Home'}</a></li>
</ul>
{elseif isset($alreadySent)}
<p>{l s='Your message has already been sent.'}</p>
<ul class="footer_links">
<li><a href="{$base_dir}"><img class="icon" alt="" src="{$img_dir}icon/home.gif"/></a><a href="{$base_dir}">{l s='Home'}</a></li>
</ul>
{else}
<p class="bold">{l s='For questions about an order or for more information about our products'}.</p>
{include file="$tpl_dir./errors.tpl"}
<form action="{$request_uri|escape:'htmlall':'UTF-8'}" method="post" class="std" enctype="multipart/form-data">
<fieldset>
<h3>{l s='Send a message'}</h3>
<p class="select">
<label for="id_contact">{l s='Subject Heading'}</label>
{if isset($customerThread.id_contact)}
{foreach from=$contacts item=contact}
{if $contact.id_contact == $customerThread.id_contact}
<input type="text" id="contact_name" name="contact_name" value="{$contact.name|escape:'htmlall':'UTF-8'}" readonly="readonly" />
<input type="hidden" name="id_contact" value="{$contact.id_contact}" />
{/if}
{/foreach}
</p>
{else}
<select id="id_contact" name="id_contact" onchange="showElemFromSelect('id_contact', 'desc_contact')">
<option value="0">{l s='-- Choose --'}</option>
{foreach from=$contacts item=contact}
<option value="{$contact.id_contact|intval}" {if isset($smarty.post.id_contact) && $smarty.post.id_contact == $contact.id_contact}selected="selected"{/if}>{$contact.name|escape:'htmlall':'UTF-8'}</option>
{/foreach}
</select>
</p>
<p id="desc_contact0" class="desc_contact"> </p>
{foreach from=$contacts item=contact}
<p id="desc_contact{$contact.id_contact|intval}" class="desc_contact" style="display:none;">
{$contact.description|escape:'htmlall':'UTF-8'}
</p>
{/foreach}
{/if}
<p class="text">
<label for="email">{l s='E-mail address'}</label>
{if isset($customerThread.email)}
<input type="text" id="email" name="from" value="{$customerThread.email|escape:'htmlall':'UTF-8'}" readonly="readonly" />
{else}
<input type="text" id="email" name="from" value="{$email|escape:'htmlall':'UTF-8'}" />
{/if}
</p>
{if !$PS_CATALOG_MODE}
{if (!isset($customerThread.id_order) || $customerThread.id_order > 0)}
<p class="text select">
<label for="id_order">{l s='Order ID'}</label>
{if !isset($customerThread.id_order) && isset($isLogged) && $isLogged == 1}
<select name="id_order" >
<option value="0">{l s='-- Choose --'}</option>
{foreach from=$orderList item=order}
<option value="{$order.value|intval}">{$order.label|escape:'htmlall':'UTF-8'}</option>
{/foreach}
</select>
{elseif !isset($customerThread.id_order) && !isset($isLogged)}
<input type="text" name="id_order" id="id_order" value="{if isset($customerThread.id_order) && $customerThread.id_order > 0}{$customerThread.id_order|escape:'htmlall':'UTF-8'}{else}{if isset($smarty.post.id_order)}{$smarty.post.id_order|escape:'htmlall':'UTF-8'}{/if}{/if}" />
{elseif $customerThread.id_order > 0}
<input type="text" name="id_order" id="id_order" value="{$customerThread.id_order|escape:'htmlall':'UTF-8'}" readonly="readonly" />
{/if}