how to add search method in the Ajax live search module?
Posted: Thu Jun 02, 2022 4:18 am
By the default Ajax live search module can search by product name and SKU. I want to make it so that the search also takes place by manufacturer. I was able to add a manufacturer to the controller so that it appears in the search results, but it does not search for this criterion.
I've looked at the controller and Java Script code in the template, but I don't see a place anywhere that would be responsible for the search parameters. Maybe someone came across and knows where to look?
This is catalog/controller file:
This is JS code in the template:
Also module has admin/controller file. But I think that he is not responsible for the search criteria. Any ideas where I can edit the search criteria?
I've looked at the controller and Java Script code in the template, but I don't see a place anywhere that would be responsible for the search parameters. Maybe someone came across and knows where to look?
This is catalog/controller file:
Code: Select all
<?php
class ControllerExtensionModuleLiveSearch extends Controller {
public function index() {
$json = array();
if (isset($this->request->get['filter_name'])) {
$search = $this->request->get['filter_name'];
} else {
$search = '';
}
if (isset($this->request->get['cat_id'])) {
$cat_id = (int)$this->request->get['cat_id'];
} else {
$cat_id = 0;
}
$tag = $search;
$description = '';
$category_id = $cat_id;
$sub_category = '';
$sort = 'p.sort_order';
$order = 'ASC';
$page = 1;
$limit = $this->config->get('module_live_search_limit');
$search_result = 0;
$error = false;
if( version_compare(VERSION, '3.0.0.0', '>=') ){
$currency_code = $this->session->data['currency'];
}
else{
$error = true;
$json[] = array(
'product_id' => 0,
'image' => null,
'name' => 'Version Error: '.VERSION,
'extra_info' => null,
'price' => 0,
'special' => 0,
'url' => '#'
);
}
if(!$error){
if (isset($this->request->get['filter_name'])) {
$this->load->model('catalog/product');
$this->load->model('tool/image');
$filter_data = array(
'filter_name' => $search,
'filter_tag' => $tag,
'filter_description' => $description,
'filter_category_id' => $category_id,
'filter_sub_category' => $sub_category,
'sort' => $sort,
'order' => $order,
'start' => ($page - 1) * $limit,
'limit' => $limit
);
$results = $this->model_catalog_product->getProducts($filter_data);
$search_result = $this->model_catalog_product->getTotalProducts($filter_data);
$image_width = $this->config->get('module_live_search_image_width') ? $this->config->get('module_live_search_image_width') : 0;
$image_height = $this->config->get('module_live_search_image_height') ? $this->config->get('module_live_search_image_height') : 0;
$title_length = $this->config->get('module_live_search_title_length');
$description_length = $this->config->get('module_live_search_description_length');
foreach ($results as $result) {
if ($result['image']) {
$image = $this->model_tool_image->resize($result['image'], $image_width, $image_height);
} else {
$image = $this->model_tool_image->resize('placeholder.png', $image_width, $image_height);
}
if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')), $currency_code);
} else {
$price = false;
}
if ((float)$result['special']) {
$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')), $currency_code);
} else {
$special = false;
}
if ($this->config->get('config_tax')) {
$tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price'], $currency_code);
} else {
$tax = false;
}
if ($this->config->get('config_review_status')) {
$rating = (int)$result['rating'];
} else {
$rating = false;
}
$json['total'] = (int)$search_result;
$json['products'][] = array(
'product_id' => $result['product_id'],
'image' => $image,
'name' => utf8_substr(strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')), 0, $title_length) . '..',
'extra_info' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $description_length) . '..',
'price' => $price,
'special' => $special,
'url' => $this->url->link('product/product', 'product_id=' . $result['product_id'])
);
}
}
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
}
Code: Select all
var live_search = {
selector: '#search input[name=\'search\']',
text_no_matches: '{{ text_empty }}',
height: '50px'
}
$(document).ready(function() {
var html = '';
html += '<div class="live-search">';
html += ' <ul>';
html += ' </ul>';
html += '<div class="result-text"></div>';
html += '</div>';
//$(live_search.selector).parent().closest('div').after(html);
$(live_search.selector).after(html);
$(live_search.selector).autocomplete({
'source': function(request, response) {
var filter_name = $(live_search.selector).val();
var cat_id = 0;
var module_live_search_min_length = '{{ module_live_search_min_length|abs }}';
if (filter_name.length < module_live_search_min_length) {
$('.live-search').css('display','none');
}
else{
var html = '';
html += '<li style="text-align: center;height:10px;">';
html += '<img class="loading" src="catalog/view/theme/default/image/loading.gif" />';
html += '</li>';
$('.live-search ul').html(html);
$('.live-search').css('display','block');
$.ajax({
url: 'index.php?route=extension/module/live_search&filter_name=' + encodeURIComponent(filter_name),
dataType: 'json',
success: function(result) {
var products = result.products;
$('.live-search ul li').remove();
$('.result-text').html('');
if (!$.isEmptyObject(products)) {
var show_image = {{ module_live_search_show_image|abs }};
var show_price = {{ module_live_search_show_price|abs }};
var show_description = {{ module_live_search_show_description|abs }};
$('.result-text').html('<a href="{{ module_live_search_href }}'+filter_name+'" class="view-all-results">{{ text_view_all_results|e }} ('+result.total+')</a>');
$.each(products, function(index,product) {
var html = '';
html += '<li>';
html += '<a href="' + product.url + '" title="' + product.name + '">';
if(product.image && show_image){
html += ' <div class="product-image"><img alt="' + product.name + '" src="' + product.image + '"></div>';
}
html += ' <div class="product-name">' + product.name ;
if(show_description){
html += '<p>' + product.extra_info + '</p>';
}
html += '</div>';
if(show_price){
if (product.special) {
html += '<div class="product-price"><span class="price">' + product.special + '</span></div>';
} else {
html += ' <div class="product-price"><span class="price">' + product.price + '</span></div>';
}
}
html += '<span style="clear:both"></span>';
html += '</a>';
html += '</li>';
$('.live-search ul').append(html);
});
} else {
var html = '';
html += '<li style="text-align: center;height:10px;">';
html += live_search.text_no_matches;
html += '</li>';
$('.live-search ul').html(html);
}
$('.live-search').css('display','block');
return false;
}
});
}
},
'select': function(product) {
$(live_search.selector).val(product.name);
}
});
$(document).bind( "mouseup touchend", function(e){
var container = $('.live-search');
if (!container.is(e.target) && container.has(e.target).length === 0)
{
container.hide();
}
});
});