Search code examples
typesfieldopencartregistration

how to add type to custom field in opencart?


I want to add code-id to registration form in opencart 2.0.3.1, but in admin panel just can choose

this type---> ( choose, choose File, Date) , my type of field is "int" and "unique" and "its Length"is 10 ?

How can I do this?


Solution

  • Custom fields in Opencart don't have validation ability, so if you need validation, you must add your desired fields manually.

    I've created a vqmod script that adds a new field with the type and length validation (it must be numeric with exactly 10 digits), the script adds new column code_melli with an UNIQE index to prevent storing a duplicated entry.

    As the question is about registration form, this script prepared in this form only, if you want to let your customer edit it from their account or echo it in admin panel, you must improve it.

    I've tested this on Opencart 2.0.3.1 with the default theme:

    <?xml version="1.0" encoding="UTF-8"?>
    <modification>
        <id>Code Melli</id>
        <version>2.0.3.1</version>
        <vqmver>2.6.0</vqmver>
        <author>darbaze.com</author>
    
        <file name="catalog/model/account/customer.php">
            <operation error="skip">
                <search position="after"><![CDATA[extends Model {]]></search>
                <add><![CDATA[
                    public function checkCodeMelli() {
                        $hasCodeMelli = false;
                        $result = $this->db->query( "DESCRIBE " . DB_PREFIX . "customer");
                        foreach ($result->rows as $row) {
                            if ($row['Field'] == 'code_melli') {
                                $hasCodeMelli = true;
                                break;
                            }
                        }
                        if (!$hasCodeMelli) {
                            $sql = "ALTER TABLE `" . DB_PREFIX . "customer` ADD `code_melli` INT(10) NULL  AFTER `lastname`,  ADD   UNIQUE  (`code_melli`) ;";
    
                            $this->db->query( $sql );
                        }
                    }
    
                    public function getTotalCustomersByCodeMelli($code_melli) {
                        $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer WHERE code_melli = '" . (int)$code_melli . "'");
    
                        return $query->row['total'];
                    }
                ]]></add>
            </operation>
            <operation error="skip">
                <search position="iafter"><![CDATA[email = '" . $this->db->escape($data['email']) . "',]]></search>
                <add><![CDATA[code_melli = '" . $this->db->escape($data['code_melli']) . "',]]></add>
            </operation>
        </file>
    
        <file name="catalog/controller/account/register.php">
            <operation error="skip">
                <search position="after"><![CDATA[$this->load->model('account/customer');]]></search>
                <add><![CDATA[
                    $this->model_account_customer->checkCodeMelli();
                ]]></add>
            </operation>
            <operation error="skip">
                <search position="before"><![CDATA[if (isset($this->error['email'])) {]]></search>
                <add><![CDATA[
                    if (isset($this->error['code_melli'])) {
                        $data['error_code_melli'] = $this->error['code_melli'];
                    } else {
                        $data['error_code_melli'] = '';
                    }
                ]]></add>
            </operation>
            <operation error="skip">
                <search position="before"><![CDATA[if (isset($this->request->post['email'])) {]]></search>
                <add><![CDATA[
                    if (isset($this->request->post['code_melli'])) {
                        $data['code_melli'] = $this->request->post['code_melli'];
                    } else {
                        $data['code_melli'] = '';
                    }
                ]]></add>
            </operation>
            <operation error="skip">
                <search position="after"><![CDATA[function validate() {]]></search>
                <add><![CDATA[
                    if (!is_numeric($this->request->post['code_melli']) || (utf8_strlen(trim($this->request->post['code_melli'])) != 10)) {
                        $this->error['code_melli'] = 'کد ملی به درستی وارد نشده است';
                    }
                    if ($this->model_account_customer->getTotalCustomersByCodeMelli($this->request->post['code_melli'])) {
                        $this->error['warning'] = 'این کد ملی قبلا ثبت شده است';
                    }
                ]]></add>
            </operation>
        </file>
    
        <file name="catalog/view/theme/*/template/account/register.tpl">
            <operation error="skip">
                <search position="before" index="1" offset="1"><![CDATA[<?php echo $entry_email; ?>]]></search>
                <add><![CDATA[
                  <div class="form-group required">
                    <label class="col-sm-2 control-label" for="input-code_melli">کد ملی</label>
                    <div class="col-sm-10">
                      <input type="code_melli" name="code_melli" value="<?php echo $code_melli; ?>" placeholder="کد ملی" id="input-code_melli" class="form-control" />
                      <?php if ($error_code_melli) { ?>
                      <div class="text-danger"><?php echo $error_code_melli; ?></div>
                      <?php } ?>
                    </div>
                  </div>
                ]]></add>
            </operation>
        </file>
    
    </modification>