En essayant d'ajouter des fonctionnalités aux groupes de clients, je suis resté bloqué lorsque j'avais besoin de la fonction de saisie semi-automatique qui obtiendra des produits afin que je puisse les ajouter à des groupes de clients spécifiques en cas de besoin qui seront affichés sur leur page Mon compte. Je ne peux pas trouver ce qui me manque pour que la saisie semi-automatique fonctionne car elle n'affiche pas la liste <li> comme la saisie semi-automatique dans Catégories (Produit -> Liens). Je peux voir la liste <li> avec Element Inspector of Product->Links Category mais pas sur mon formulaire de groupes de clients personnalisé.

Pour donner plus de lumière, voici ce que j'ai fait jusqu'à présent:

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;
}
0
milo 14 févr. 2020 à 06:14

1 réponse

Meilleure réponse

Si vous avez besoin de la saisie semi-automatique pour un groupe de clients, pourquoi interrogez-vous des produits ? Pour le groupe de clients, ajoutez une fonction au contrôleur, par exemple au nouveau fichier de votre module : 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));
    }

À ce fichier, ajoutez également à la section public function index() ceci :

$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']
                    );
                }
            }

Ensuite, vous n'avez pas besoin de créer de fichier modèle pour que les groupes de clients utilisent simplement l'existant et chargez-le comme vous pouvez le voir ci-dessus :$this->load->model('customer/customer_group');

Dans votre fichier modèle admin/view/template/extension/module/customer_group_module.twig ajoutez :

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

Et avant {{ footer }}, ajoutez jquery pour les groupes de clients et non pour le produit :

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

Ceci est testé et fonctionne.

0
K. B. 14 févr. 2020 à 06:04