Opencart 3.0.2.3 - 添加了 Autocomplete() 功能 customer_group.php

Opencart 3.0.2.3 - Autocomplete() function added to customer_group.php

尝试向客户组添加一些功能我在需要自动完成功能时遇到了困难,该功能将获取产品,因此我可以在需要时将它们添加到特定的客户组,这些产品将显示在他们的“我的帐户”页面上。为了让自动完成工作,我找不到我缺少的东西,因为它没有像类别(产品 --> 链接)中的自动完成那样显示列表 <li>。我可以在产品-> 链接类别的元素检查器中看到 <li> 列表,但在我的自定义客户组表单上看不到。

为了提供更多的光线,这是我目前所做的:

admin/controller/customer/customer_group.php

public function autocomplete() {
        $json = array();

        if (isset($this->request->get['filter_name'])) {
            $this->load->model('customer/customer_group');

            $filter_data = array(
                'filter_name' => $this->request->get['filter_name'],
                'sort'        => 'name',
                'order'       => 'ASC',
                'limit'       => 5
            );

            $this->load->model('catalog/product');

            $results = $this->model_catalog_product->getProducts($filter_data);

            foreach ($results as $result) {
                $json[] = array(
                    'product_id' => $result['product_id'],
                    'name'       => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'))
                );
            }
        }

        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }

admin/view/template/customer/customer_group_form.twig

    <div class="form-group">
        <label class="col-sm-2 control-label" for="input-product"><span data-toggle="tooltip" title="{{ help_product }}">{{ entry_product }}</span></label>
        <div class="col-sm-10">
          <input type="text" name="product" value="" placeholder="{{ entry_product }}" id="input-product" class="form-control"/>
          <div id="product-customer" class="well well-sm" style="height: 150px; overflow: auto;"> {% for product in products %}
            <div id="product-customer{{ product.product_id }}"><i class="fa fa-minus-circle"></i> {{ product.name }}
              <input type="hidden" name="products[]" value="{{ product.product_id }}"/>
            </div>
          {% endfor %}</div>
        </div>
      </div>

...

<script type="text/javascript"><!--
  $('input[name=\'product\']').autocomplete({
      'source': function(request, response) {
        $.ajax({
            url: 'index.php?route=customer/customer_group/autocomplete&user_token={{ user_token }}&filter_name=' +  encodeURIComponent(request),
            dataType: 'json',
            success: function(json) {
                json.unshift({
                    product_id: 0,
                    name: '{{ text_none }}'
                });

                response($.map(json, function(item) {
                    return {
                        label: item['name'],
                        value: item['product_id']
                    }
                }));
            }
        });
    },
      'select': function(item) {
          $('input[name=\'product\']').val('');

          $('#product-customer' + item['value']).remove();

          $('#product-customer').append('<div id="product-customer' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="products[]" value="' + item['value'] + '" /></div>');
      }
  });

  $('#product-customer').delegate('.fa-minus-circle', 'click', function() {
      $(this).parent().remove();
  });
  //--></script>

admin/model/customer/customer_group.php

public function getProduct($product_id) {
    $query = $this->db->query("SELECT DISTINCT *, pd.name AS name, p.image, m.name AS manufacturer, (SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1) AS discount, (SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special, (SELECT points FROM " . DB_PREFIX . "product_reward pr WHERE pr.product_id = p.product_id AND pr.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "') AS reward, (SELECT ss.name FROM " . DB_PREFIX . "stock_status ss WHERE ss.stock_status_id = p.stock_status_id AND ss.language_id = '" . (int)$this->config->get('config_language_id') . "') AS stock_status, (SELECT wcd.unit FROM " . DB_PREFIX . "weight_class_description wcd WHERE p.weight_class_id = wcd.weight_class_id AND wcd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS weight_class, (SELECT lcd.unit FROM " . DB_PREFIX . "length_class_description lcd WHERE p.length_class_id = lcd.length_class_id AND lcd.language_id = '" . (int)$this->config->get('config_language_id') . "') AS length_class, (SELECT AVG(rating) AS total FROM " . DB_PREFIX . "review r1 WHERE r1.product_id = p.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating, (SELECT COUNT(*) AS total FROM " . DB_PREFIX . "review r2 WHERE r2.product_id = p.product_id AND r2.status = '1' GROUP BY r2.product_id) AS reviews, p.sort_order FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) LEFT JOIN " . DB_PREFIX . "manufacturer m ON (p.manufacturer_id = m.manufacturer_id) WHERE p.product_id = '" . (int)$product_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'");

    if ($query->num_rows) {
        return array(
            'product_id'       => $query->row['product_id'],
            'name'             => $query->row['name'],
            'description'      => $query->row['description'],
            'meta_title'       => $query->row['meta_title'],
            'meta_description' => $query->row['meta_description'],
            'meta_keyword'     => $query->row['meta_keyword'],
            'tag'              => $query->row['tag'],
            'model'            => $query->row['model'],
            'sku'              => $query->row['sku'],
            'upc'              => $query->row['upc'],
            'ean'              => $query->row['ean'],
            'jan'              => $query->row['jan'],
            'isbn'             => $query->row['isbn'],
            'mpn'              => $query->row['mpn'],
            'location'         => $query->row['location'],
            'quantity'         => $query->row['quantity'],
            'stock_status'     => $query->row['stock_status'],
            'image'            => $query->row['image'],
            'manufacturer_id'  => $query->row['manufacturer_id'],
            'manufacturer'     => $query->row['manufacturer'],
            'price'            => ($query->row['discount'] ? $query->row['discount'] : $query->row['price']),
            'special'          => $query->row['special'],
            'reward'           => $query->row['reward'],
            'points'           => $query->row['points'],
            'tax_class_id'     => $query->row['tax_class_id'],
            'date_available'   => $query->row['date_available'],
            'weight'           => $query->row['weight'],
            'weight_class_id'  => $query->row['weight_class_id'],
            'length'           => $query->row['length'],
            'width'            => $query->row['width'],
            'height'           => $query->row['height'],
            'length_class_id'  => $query->row['length_class_id'],
            'subtract'         => $query->row['subtract'],
            'rating'           => round($query->row['rating']),
            'reviews'          => $query->row['reviews'] ? $query->row['reviews'] : 0,
            'minimum'          => $query->row['minimum'],
            'sort_order'       => $query->row['sort_order'],
            'status'           => $query->row['status'],
            'date_added'       => $query->row['date_added'],
            'date_modified'    => $query->row['date_modified'],
            'viewed'           => $query->row['viewed']
        );
    } else {
        return false;
    }
}

public function getProducts($data = array()) {
    $sql = "SELECT pd.name, p.product_id FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store pts ON (p.product_id = pts.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND pts.store_id = '" . (int)$this->config->get('config_store_id') . "'";

    if (!empty($data['filter_name'])) {
        $sql .= " AND pd.name LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
    }

    $sql .= " GROUP BY p.product_id";

    $sort_data = array(
        'name',
        'sort_order'
    );

    if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
        $sql .= " ORDER BY " . $data['sort'];
    } else {
        $sql .= " ORDER BY sort_order";
    }

    if (isset($data['order']) && ($data['order'] == 'DESC')) {
        $sql .= " DESC";
    } else {
        $sql .= " ASC";
    }

    $query = $this->db->query($sql);

    return $query->rows;
}

如果您需要为客户组自动完成,为什么要查询产品? 对于客户组向控制器添加功能,例如添加到新的模块文件:admin/controller/extension/module/customer_group_module.php

public function autocomplete() {
        $json = array();

        if (isset($this->request->get['filter_customer_group'])) {
            $this->load->model('customer/customer_group');

            $results = $this->model_customer_customer_group->getCustomerGroups();

            foreach ($results as $result) {
                $json[] = array(
                    'customer_group_id' => $result['customer_group_id'],
                    'name'              => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'))
                );
            }
        }

        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }

在此文件的 public function index() 部分也添加以下内容:

$data['user_token'] = $this->session->data['user_token'];

if (isset($this->request->post['customer_group_module_customer_group'])) {
                $data['customer_group_module_customer_group'] = $this->request->post['customer_group_module_customer_group'];
            } elseif ($this->config->get('customer_group_module_customer_group')) {
                $data['customer_group_module_customer_group'] = $this->config->get('customer_group_module_customer_group');
            } else {
                $data['customer_group_module_customer_group'] = array();
            }

            $data['customer_groups'] = array();

            $this->load->model('customer/customer_group');

            foreach ($data['customer_group_module_customer_group'] as $customer_group_id) {
                $customer_group_info = $this->model_customer_customer_group->getCustomerGroup($customer_group_id);

                if ($customer_group_info) {
                    $data['customer_groups'][] = array(
                        'customer_group_id' => $customer_group_info['customer_group_id'],
                        'name'              => $customer_group_info['name']
                    );
                }
            }

接下来您不需要创建任何模型文件来让客户组只使用现有的并加载它,如上所示:$this->load->model('customer/customer_group');

在你的模板文件中 admin/view/template/extension/module/customer_group_module.twig 添加:

 <div class="form-group">
                <label class="col-sm-2 control-label" for="customer-group"><span data-toggle="tooltip" title="{{ help_customer_group }}">{{ entry_customer_group }}</span></label>
                <div class="col-sm-10">
                  <input type="text" name="customer_group" value="" placeholder="{{ entry_customer_group }}" id="customer-group" class="form-control" />
                  <div id="module-customer-group" class="well well-sm" style="height: 150px; overflow: auto;">
                    {% for customer_group in customer_groups %} 
                      <div id="customer-group{{ customer_group['customer_group_id'] }}"><i class="fa fa-minus-circle"></i> {{ customer_group['name'] }} 
                        <input type="hidden" name="customer_group_module_customer_group[]" value="{{ customer_group['customer_group_id'] }}" />
                      </div>
                    {% endfor %} 
                  </div>
                </div>
              </div>

{{ footer }} 之前为客户组而不是产品添加 jquery:

<script type="text/javascript"><!--
    $('input[name="customer_group"]').autocomplete({
        source: function(request, response) {
            $.ajax({
                url: 'index.php?route=extension/module/customer_group_module/autocomplete&user_token={{ user_token }}&filter_customer_group=' +  encodeURIComponent(request),
                dataType: 'json',
                success: function(json) {
                    response($.map(json, function(item) {
                        return {
                            label: item['name'],
                            value: item['customer_group_id']
                        }
                    }));
                }
            });
        },
        select: function(item) {
            $('input[name=\'customer_group\']').val('');

            $('#module-customer-group' + item['value']).remove();

            $('#module-customer-group').append('<div id="module-customer-group' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="customer_group_module_customer_group[]" value="' + item['value'] + '" /></div>');
        }
    });

    $('#module-customer-group').delegate('.fa-minus-circle', 'click', function() {
        $(this).parent().remove();
    });
//--></script>

这已经过测试并且可以正常工作。