How to add Telephone field in OpenCart 2.0.2.0 contact form

Crtvca Mag picture Crtvca Mag · Apr 21, 2015 · Viewed 9.6k times · Source

How can I add extra input fields in OpenCart contact form (under information)? Specially I want to add a telephone number field in my contact form and I've followed a tutorial but it didn't work for me. Is there any alternative?

Answer

QDappnet picture QDappnet · May 9, 2015

I know it's possible that you already resolved this issue, but this is for those who still want to add custom field to contact form.

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

  1. Buy an extension (as of 5/9/2015, it seems that I can't find one that modify directly Contact Us form)
  2. Do it yourself: To do it yourself you can follow your tutorial link or follow instructions below

To add custom Telephone field to "Contact Form" in OpenCart 2.0, you would need to edit 3 files:

  1. \catalog\language\english\information\contact.php
  2. \catalog\controller\information\contact.php
  3. \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']);

3. Edit template file: \catalog\view\theme[YourThemeName]\template\information\contact.tpl

a. Under 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 update above 3 files, just upload to your server and test. Good luck!