In OpenCart 2.0 default Contact Us page (/index.php?route=information/contact) – Contact Form has only 3 fields: Your Name, E-Mail Address, and Enquiry
To add custom fields to Contact Form, you can
- Buy an extension (as of 5/9/2015, it seems that I can’t find one that modify directly Contact Us form) – UPDATE as of 4/25/2016, there are quite some extensions available.
- Do it yourself: To do it yourself you can follow this tutorial link or follow instructions below to add a Telephone is the form (if you want to add more fields, just add more similar codes)
To add custom Telephone field to “Contact Form” in OpenCart 2.0, you would need to edit 3 files:
- \catalog\language\english\information\contact.php
- \catalog\controller\information\contact.php
- \catalog\view\theme[YourThemeName]\template\information\contact.tpl
[YourThemeName] = Whatever theme that you selected for your store, default is “default” (You can verify or set it here: /Admin => Systems => Settings => Select your store and click Edit => Store tab => Default Layout)
1. Edit language file: \catalog\language\english\information\contact.php
a. Under line:
$_['entry_email'] = 'E-Mail Address';
Add code:
$_['entry_phone'] = 'Telephone';
b. Under line
$_['error_email'] = 'E-Mail Address does not appear to be valid!';
Add code:
$_['error_phone'] = 'Telephone is required!';
2. Edit control file: \catalog\controller\information\contact.php
a. Under code:
$data['entry_email'] = $this->language->get('entry_email');
Add code:
$data['entry_phone'] = $this->language->get('entry_phone');
b. Under code
if (isset($this->error['email'])) {
$data['error_email'] = $this->error['email'];
} else {
$data['error_email'] = '';
}
Add code
if (isset($this->error['phone'])) {
$data['error_phone'] = $this->error['phone'];
} else {
$data['error_phone'] = '';
}
c. Under code:
if (!preg_match('/^[^\@]+@.*.[a-z]{2,15}$/i', $this->request->post['email'])) {
$this->error['email'] = $this->language->get('error_email');
}
Add code:
if ((utf8_strlen($this->request->post['phone']) < 1)) {
$this->error['phone'] = $this->language->get('error_phone');
}
d. FIND code
$mail->setText($this->request->post['enquiry']);
UPDATE code to
$mail->setText($this->request->post['enquiry'] . $mail->newline . 'Telephone: ' . $this->request->post['phone']);
e. Under code: 1/4/2016 NEW CODES ADDED TO FIX Undefined variable: phone
if (isset($this->request->post['email'])) { $data['email'] = $this->request->post['email']; } else { $data['email'] = $this->customer->getEmail(); }
Add code:
if (isset($this->request->post['phone'])) { $data['phone'] = $this->request->post['phone']; } else { $data['phone'] = ''; }
3. Edit template file: \catalog\view\theme[YourThemeName]\template\information\contact.tpl
a. Under line:
Will update this section… WordPress auto remove my php codes even thought I pasted as text only or use <pre> tag still not working
After update above 3 files, just upload to your server and test. Good luck!
I posted an answer in stackoverflow site
August 12, 2015 at 11:41 am
Thanks but I get this error after adding
Notice: Undefined variable: phone in /home/clearanw/public_html/system/modification/catalog/view/theme/journal2/template/information/contact.tpl on line 148
LikeLike
September 1, 2016 at 6:26 pm
Try to refresh modification folder
http://www.templatemonster.com/help/opencart-2-x-how-to-manage-modifications.html
LikeLike
January 5, 2018 at 4:33 pm
PHP Notice: Undefined variable: enquiry in /home/n19des5/public_html/catalog/view/theme/pav_mode/template/information/contact.tpl on line 184
This is what I get – Thoughts?
LikeLiked by 1 person
December 11, 2015 at 6:32 pm
Hi and thaks for helping with the code.
However i’m getting theses two errors on contact form:
Notice: Undefined variable: entry_phone in /home/xxx/www/catalog/view/theme/default/template/information/contact.tpl on line 126
Notice: Undefined variable: phone in /home/xxx/www/catalog/view/theme/default/template/information/contact.tpl on line 128
Notice: Undefined variable: error_phone in /home/xxx/www/catalog/view/theme/default/template/information/contact.tpl on line 129
But all of these variables have been pasted in the files according to your post.
Can you
LikeLike
December 11, 2015 at 7:20 pm
Email me those 3 files, I’ll check for you.
LikeLike
August 3, 2016 at 7:20 am
Extension->Modifications … Refresh it.
LikeLike
December 18, 2015 at 12:36 pm
I also having the same problem as above. Please post solution here.
LikeLike
December 24, 2015 at 5:01 pm
Hey, thanks for your answer!
I actually found an automated ocmod extension that adds custom fields to the contact page:
https://www.opencart.com/index.php?route=extension/extension/info&extension_id=24112&token=2faf27cd71b868204d7e271df42d08cd
It works fine since the creation of new fields to the exhibition of the fields in front-end. However the content of the fields created are not being received in my mailbox.
I’m using OC 2.0.1.1
The developer never answer my question. So I thought you could take a loook at this extension as it seems to be an issue with the mail function.
I will donate some for the fix.
Just let me know your paypal e-mail.
LikeLike
January 4, 2016 at 10:41 pm
Hello?
LikeLike
November 22, 2016 at 8:52 pm
Most helpful code modification tut out here that I’ve seen. However, in the controller\information file, email and telephone validation is almost non-existent. I’ve looked for a while already, but no luck in finding any good validation methods for email or phone number.
I’m using OC 2.3.0.2
I tried a couple regex’s and editing the filter_var function but no luck.
Any suggestions?
LikeLike
December 16, 2016 at 7:24 am
3. Edit template file: \catalog\view\theme[YourThemeName]\template\information\contact.tpl
a. Under line:
<input type="text" name="email" value="” id=”input-email” class=”form-control” />
Add the following Code :
<input type="text" name="phone" value="” id=”input-phone” class=”form-control” />
LikeLike
August 3, 2017 at 10:02 am
I have successfully added Custom Field to Contact Form in Contact Us Page in but customer contact number not shown up in received email.
LikeLike
August 3, 2017 at 10:44 am
sorry by mistack I send enquiry It’s my own error smtp not working for me. When I am using it with mail protocol it’s works fine. but I have one issue recieved mail not shows cusomer email address & it will display all in single line…no space or new line between enquiry and Telephone number.
LikeLike
March 31, 2020 at 8:49 pm
May I simply say what a comfort to uncover somebody that actually knows
what they’re discussing on the internet. You actually
realize how to bring a problem to light and make it important.
More people must check this out and understand this side
of the story. I was surprised that you aren’t more popular
given that you surely have the gift.
LikeLike