opencart

add a required field to checkout/register in OpenCart


Opencart 1.5.5.1

I added a new field to may checkout register page:

-added to catalog/view/theme/default/template/checkout/register.tpl:

  <span class="required">*</span> <?php echo $entry_cnp; ?><br />
  <input type="text" name="cnp" value="" class="large-field" />
  <br />
  <br />

and to catalog/controller/checkout/register.php:

$this->data['entry_cnp'] = $this->language->get('entry_cnp');

and

if ((utf8_strlen($this->request->post['cnp']) < 3) || (utf8_strlen($this->request->post['cnp']) > 32)) {
                $json['error']['cnp'] = $this->language->get('error_cnp');
            }

If I register on checkout it works great, my problem is when I do not complete this field: I'm not allowed to proceed further but I do not get an error message like on the other requested fields


Solution

  • First add 'error_cnp' variable in language file if you had not added it yet like this

    $_['error_cnp'] = 'some error text';
    

    Second, in catalog/view/theme/default/template/checkout/checkout.tpl:

    find

    $('#button-register').live('click', function() {
     $.ajax({
            url: 'index.php?route=checkout/register/validate',
            // some code
            success: function() {
    

    and inside this success function paste following lines

    if (json['error']['cnp']) {
        $('#payment-address input[name=\'cnp\'] + br').after('<span class="error">' + json['error']['cnp'] + '</span>');
    }
    

    in between the similar looking lines (inside the else if (json['error']) { )