How to add a phone field to a contact form OpenCart 2.0.2.0
I know it is possible that you have already solved this problem, but this is for those who still want to add a custom field to a contact form.
In OpenCart 2.0, the default Contact Page (/index.php?route=information/contact) - Contact form only has 3 fields: your name, email and request
To add custom fields to your contact form, you can
- Buy an extension (as of 5/9/2015, I can't seem to find the one that changes directly. Contact us form)
- Do it yourself: for this you can follow the tutorial link or follow the instructions below
To add a custom "Phone" field to the "Contact Form" in OpenCart 2.0, you will need to edit 3 files:
- \ directory \ language \ english \ info \ contact.php
- \ Directory \ controller \ information \ contact.php
- \ directory \ view \ theme [YourThemeName] \ template \ information \ contact.tpl
[YourThemeName] = Whatever theme you choose for your store, the default is the default, (You can check or set it here: / Admin => Systems => Settings => Select your store and click Edit => Save Tab = > Default Layout)
1. Change the language file: \ catalog \ language \ english \ information \ contact.php
and. Below the line:
$_['entry_email'] = 'E-Mail Address';
Add code:
$_['entry_phone'] = 'Telephone';
b. Under the line
$_['error_email'] = 'E-Mail Address does not appear to be valid!';
Add code:
$_['error_phone'] = 'Telephone is required!';
2. Modify the control file: \ catalog \ controller \ information \ contact.php
and. Below the code:
$data['entry_email'] = $this->language->get('entry_email');
Add code:
$data['entry_phone'] = $this->language->get('entry_phone');
b. Under the 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'] = '';
}
from. Below the 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');
}
FIND CODE
$mail->setText($this->request->post['enquiry']);
UPDATE code for
$mail->setText($this->request->post['enquiry'] . $mail->newline . 'Telephone: ' . $this->request->post['phone']);
3. Edit the template file: \ catalog \ view \ theme [YourThemeName] \ template \ information \ contact.tpl
and. Below the line:
<div class="form-group required">
<label class="col-sm-2 control-label" for="input-email"><?php echo $entry_email; ?></label>
<div class="col-sm-10">
<input type="text" name="email" value="<?php echo $email; ?>" id="input-email" class="form-control" />
<?php if ($error_email) { ?>
<div class="text-danger"><?php echo $error_email; ?></div>
<?php } ?>
</div>
</div>
Add code:
<div class="form-group required">
<label class="col-sm-2 control-label" for="input-phone"><?php echo $entry_phone; ?></label>
<div class="col-sm-10">
<input type="text" name="phone" value="<?php echo $phone; ?>" id="input-phone" class="form-control" />
<?php if ($error_phone) { ?>
<div class="text-danger"><?php echo $error_phone; ?></div>
<?php } ?>
</div>
</div>
After updating above 3 files, just upload to your server and check. Good luck!
source to share
In OC 2.x, by default, the phone number is available for the contact template. I'm assuming you've upgraded from an older version and kept your old theme?
To add your phone number, open: catalog / view / theme / YOUR TOPIC / template / information / contact.tpl
And use the following to add the phone information (which will come from the phone # assigned in the store settings).
Display the language string for "Phone":
<?php echo $text_telephone; ?>
Display phone number from settings:
<?php echo $telephone; ?>
source to share
Under File - \ catalog \ controller \ information \ contact.php: NEW CODES TO BE ADDED FOR USE Undefined variable: phone
In the Code section:
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'] = '';
}
source to share
Thanks for every one...
Actually I faced the same problem but when I used these above mentioned codes in 3 different places I got another problem which shows "<b>Notice</b>: Undefined variable: phone in <b>/home/gwbpsuxl/public_html/catalog/view/theme/default/template/information/contact.tpl</b> on line <b>127</b>" this error in **Phone option**.
I used this code "if (isset($this->request->post['phone'])) {
$data['phone'] = $this->request->post['phone'];
} else {
$data['phone'] = $this->customer->getTelephone();
}" 2 times in **contact.php** file
1 is above from it and 2 is below this code "$data['locations'][] = array(
'location_id' => $location_info['location_id'],
'name' => $location_info['name'],
'address' => nl2br($location_info['address']),
'geocode' => $location_info['geocode'],
'telephone' => $location_info['telephone'],
'fax' => $location_info['fax'],
'image' => $image,
'open' => nl2br($location_info['open']),
'comment' => $location_info['comment']
);
}
}"
edit it and make it clear.
source to share