Рекомендуемые

This commit is contained in:
Konstantin
2026-05-30 14:59:49 +03:00
parent 0e8a0f3b0c
commit 6a847ab7b5
4 changed files with 80 additions and 0 deletions
@@ -156,6 +156,27 @@ class ControllerExtensionModuleFeatured extends Controller {
$this->response->setOutput($this->load->view('extension/module/featured', $data)); $this->response->setOutput($this->load->view('extension/module/featured', $data));
} }
public function random() {
$json = array();
$this->load->model('catalog/product');
$limit = isset($this->request->get['limit']) ? (int)$this->request->get['limit'] : 5;
$exclude = isset($this->request->get['exclude']) ? $this->request->get['exclude'] : array();
$results = $this->model_catalog_product->getRandomProducts($limit, $exclude);
foreach ($results as $result) {
$json[] = array(
'product_id' => $result['product_id'],
'name' => html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')
);
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
protected function validate() { protected function validate() {
if (!$this->user->hasPermission('modify', 'extension/module/featured')) { if (!$this->user->hasPermission('modify', 'extension/module/featured')) {
$this->error['warning'] = $this->language->get('error_permission'); $this->error['warning'] = $this->language->get('error_permission');
@@ -13,11 +13,15 @@ $_['text_edit'] = 'Редактирование';
// Entry // Entry
$_['entry_name'] = 'Название модуля'; $_['entry_name'] = 'Название модуля';
$_['entry_product'] = 'Товары'; $_['entry_product'] = 'Товары';
$_['entry_random'] = 'Случайные товары';
$_['entry_limit'] = 'Лимит'; $_['entry_limit'] = 'Лимит';
$_['entry_width'] = 'Ширина'; $_['entry_width'] = 'Ширина';
$_['entry_height'] = 'Высота'; $_['entry_height'] = 'Высота';
$_['entry_status'] = 'Статус'; $_['entry_status'] = 'Статус';
// Button
$_['button_random'] = 'Добавить случайные';
// Help // Help
$_['help_product'] = '(Автозаполнение)'; $_['help_product'] = '(Автозаполнение)';
+14
View File
@@ -924,6 +924,20 @@ class ModelCatalogProduct extends Model {
return $query->row['total']; return $query->row['total'];
} }
public function getRandomProducts($limit = 5, $exclude = array()) {
$sql = "SELECT p.product_id, pd.name FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1'";
if (!empty($exclude)) {
$sql .= " AND p.product_id NOT IN (" . implode(',', array_map('intval', $exclude)) . ")";
}
$sql .= " ORDER BY RAND() LIMIT " . (int)$limit;
$query = $this->db->query($sql);
return $query->rows;
}
public function getTotalProductsByLayoutId($layout_id) { public function getTotalProductsByLayoutId($layout_id) {
$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product_to_layout WHERE layout_id = '" . (int)$layout_id . "'"); $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product_to_layout WHERE layout_id = '" . (int)$layout_id . "'");
@@ -47,6 +47,17 @@
</div> </div>
</div> </div>
</div> </div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-random">{{ entry_random }}</label>
<div class="col-sm-10">
<div class="input-group">
<input type="number" name="random_count" value="5" placeholder="{{ entry_random }}" id="input-random" class="form-control" min="1" />
<span class="input-group-btn">
<button type="button" id="button-random" data-toggle="tooltip" title="{{ button_random }}" class="btn btn-primary"><i class="fa fa-random"></i> {{ button_random }}</button>
</span>
</div>
</div>
</div>
<div class="form-group"> <div class="form-group">
<label class="col-sm-2 control-label" for="input-limit">{{ entry_limit }}</label> <label class="col-sm-2 control-label" for="input-limit">{{ entry_limit }}</label>
<div class="col-sm-10"> <div class="col-sm-10">
@@ -123,5 +134,35 @@ $('input[name=\'product_name\']').autocomplete({
$('#featured-product').delegate('.fa-minus-circle', 'click', function() { $('#featured-product').delegate('.fa-minus-circle', 'click', function() {
$(this).parent().remove(); $(this).parent().remove();
}); });
$('#button-random').on('click', function() {
var limit = parseInt($('#input-random').val()) || 5;
var exclude = [];
$('#featured-product input[name=\'product[]\']').each(function() {
exclude.push($(this).val());
});
$.ajax({
url: 'index.php?route=extension/module/featured/random&user_token={{ user_token }}&limit=' + limit,
type: 'GET',
dataType: 'json',
traditional: true,
data: { exclude: exclude },
beforeSend: function() {
$('#button-random').button('loading');
},
complete: function() {
$('#button-random').button('reset');
},
success: function(json) {
$.each(json, function(i, item) {
$('#featured-product' + item['product_id']).remove();
$('#featured-product').append('<div id="featured-product' + item['product_id'] + '"><i class="fa fa-minus-circle"></i> ' + item['name'] + '<input type="hidden" name="product[]" value="' + item['product_id'] + '" /></div>');
});
$('#featured-product div input').before(' <i class="fa fa-hand-grab-o handle"></i> ');
}
});
});
//--></script></div> //--></script></div>
{{ footer }} {{ footer }}