如何在opencart中向自定义字段添加类型?

时间:2016-12-21 06:02:27

标签: types field opencart registration

我想在 opencart 2.0.3.1 中向注册表单添加code-id,但在管理面板中可以选择

  

这种类型---> (选择,选择文件,日期),我的字段类型为"int""unique""its Length"为10?

我该怎么做?

1 个答案:

答案 0 :(得分:0)

Opencart中的自定义字段没有验证功能,因此如果您需要验证,则必须手动添加所需的字段。

我创建了一个vqmod脚本,它添加了一个带有类型和长度验证的新字段(它必须是10位数的数字),脚本会添加一个code_melli的新列UNIQE 1}}索引,以防止存储重复的条目。

由于问题与注册表单有关,此脚本仅以此表单形式编写,如果您希望让客户通过其帐户对其进行编辑或在管理面板中回显,则必须对其进行改进。

我在Opencart 2.0.3.1上使用默认主题测试了这个:

<?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>