Всем привет. Подскажите пожалуйста как сделать поиск в Ajax Search не только по названию товара а и по модели?
Скрипты контроллера поиска ajax и js. Может кому еще будет полезно решить эту проблему.
public function ajax()
{
// Contains results
$data = array();
if( isset($this->request->get['keyword']) ) {
// Parse all keywords to lowercase
$keywords = strtolower( $this->request->get['keyword'] );
// Perform search only if we have some keywords
if( strlen($keywords) >= 3 ) {
$parts = explode( ' ', $keywords );
$add = '';
// Generating search
foreach( $parts as $part ) {
$add .= ' AND LOWER(name) LIKE "%' . $this->db->escape($part) . '%"';
}
$add = substr( $add, 4 );
$sql = 'SELECT pd.product_id, pd.name FROM ' . DB_PREFIX . 'product_description AS pd ';
$sql .= 'LEFT JOIN ' . DB_PREFIX . 'product AS p ON p.product_id = pd.product_id ';
$sql .= 'LEFT JOIN ' . DB_PREFIX . 'product_to_store AS p2s ON p2s.product_id = pd.product_id ';
$sql .= 'WHERE ' . $add . ' AND p.status = 1 ';
$sql .= 'AND pd.language_id = ' . (int)$this->config->get('config_language_id');
$sql .= ' AND p2s.store_id = ' . (int)$this->config->get('config_store_id');
$sql .= ' LIMIT 15';
$res = $this->db->query( $sql );
if( $res ) {
$data = ( isset($res->rows) ) ? $res->rows : $res->row;
// For the seo url stuff
$this->load->model('tool/seo_url');
$basehref = HTTP_SERVER . 'index.php?route=product/product&keyword=' . $this->request->get['keyword'] . '&product_id=';
foreach( $data as $key => $values ) {
$data[$key] = array(
'name' => htmlspecialchars_decode( $values['name'], ENT_QUOTES ),
'href' => $this->model_tool_seo_url->rewrite( $basehref . $values['product_id'] )
);
}
}
}
}
echo json_encode( $data );
}
//<![CDATA[
function doLiveSearch( keywords ) {
$('#search_results').remove();
if( keywords == '' || keywords.length < 2 ) {
return false;
}
$.ajax({url: 'index.php?route=product/search/ajax&keyword=' + keywords, dataType: 'json', success: function(result) {
if( result.length > 0 ) {
var eList = document.createElement('ul');
eList.id = 'search_results';
var eListElem;
var eLink;
for( var i in result ) {
eListElem = document.createElement('li');
eLink = document.createElement('a');
eLink.appendChild(document.createTextNode(result[i].name));
eLink.href = result[i].href; //'index.php?route=product/product&product_id=' + result[i].product_id;
eListElem.appendChild(eLink);
eList.appendChild(eListElem);
}
$('#search').append(eList);
}
}});
return true;
}
$(document).ready(function(){
$('#filter_keyword').keyup(function(){
doLiveSearch(this.value);
}).focus(function(){
doLiveSearch(this.value);
}).blur(function(){
window.setTimeout("$('#search_results').remove()", 1500);
});
});
//]]>