phpopencart

Require 4 digits postcode


I have to make postcode field to accept just at least 4 digits input. I work with opencart 4 and journal theme.

I tried to modify following line in /public_html/catalog/controller/checkout/shipping_address.php

if ($country_info && $country_info['postcode_required'] && (oc_strlen($this->request->post['postcode']) < 4 || oc_strlen($this->request->post['postcode']) > 10)) {
                $json['error']['postcode'] = $this->language->get('error_postcode');
            }

and in corresponding catalog\view\theme\journal3\template\checkout\shipping_address.twig

    <div class="form-group required">
      <label class="col-sm-2 control-label" for="input-shipping-postcode">{{ entry_postcode }}</label>
      <div class="col-sm-10">
        <input type="text" name="postcode" value="{{ postcode }}" placeholder="{{ entry_postcode }}" id="input-shipping-postcode" class="form-control" required>
      </div>
    </div>

But any modifiations I add to the controller dont seem to work even after i cleared cache.


Solution

  • First you need to check from admin that postcode is required for all of those countries where you are delivering. Otherwise min and max lenghts are not working. You can find that setting from System-->Localisation-->Countries.

    You have to modify three different files to get that work:

    Row numbers may be different if you are using some extensions.

    Heres how to modify your shipping_address.php file.

    if ($country_info && $country_info['postcode_required'] && (oc_strlen($this->request->post['postcode']) < 5 || oc_strlen($this->request->post['postcode']) > 10)) {
                $json['error']['postcode'] = $this->language->get('error_postcode');
            }
    

    Modification to register.php row 312:

    if ($payment_country_info && $payment_country_info['postcode_required'] && (oc_strlen($this->request->post['payment_postcode']) < 5 || oc_strlen($this->request->post['payment_postcode']) > 10)) {
                    $json['error']['payment_postcode'] = $this->language->get('error_postcode');
                }
    

    Modification to register.php row 360:

    if ($shipping_country_info && $shipping_country_info['postcode_required'] && (oc_strlen($this->request->post['shipping_postcode']) < 5 || oc_strlen($this->request->post['shipping_postcode']) > 10)) {
                    $json['error']['shipping_postcode'] = $this->language->get('error_postcode');
                }
    

    Modification to payment_address.php row 126:

    if ($country_info && $country_info['postcode_required'] && (oc_strlen($this->request->post['postcode']) < 5 || oc_strlen($this->request->post['postcode']) > 10)) {
                $json['error']['postcode'] = $this->language->get('error_postcode');
            }
    

    If you want to have minimum lenght af postcode to be 4 digits, you need to replace that original number two with 5. And don't forget to update your language files to match the minimum lenght of postcode.