How to add Telephone field in OpenCart 2.0.2.0 con

2019-07-22 12:08发布

问题:

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?

回答1:

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!



回答2:

In OC 2.x the telephone number is available to the contact template by default. I'm guessing you upgraded from an older version and kept your old theme?

To add your telephone number open up: catalog/view/theme/YOUR THEME/template/information/contact.tpl

And use the following to add telephone info (which will come from the phone # assigned in store settings).

Display the language string for "Telephone":

<?php echo $text_telephone; ?>

Display the telephone number from settings:

<?php echo $telephone; ?>


回答3:

Under File - \catalog\controller\information\contact.php : NEW CODES ADDED TO FIX Undefined variable: phone

Under Code :

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'] = '';
}


回答4:

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']
                );
            }
        }"

so edit it and make it clear.