制造商

时间:2017-03-23 08:28:01

标签: php search opencart

我正在尝试在标题搜索框中启用制造商搜索,我正在使用OC 2.2.0。在autosearch.php中,我补充道:

    if (($this->config->get('autosearch_manufacturer')) > 0 ) $add .= ' OR LOWER(m.name) LIKE "%' . $this->db->escape($part) . '%"';

但它不起作用。整个autosearch.php文件是:

    <?php  
class ControllerModuleAutosearch extends Controller {

    public function ajax_asr()
    {
    function get_sum($stra)
  {
   $var1 = array_flip(get_html_translation_table());
   $stra = strtr($stra, $var1);
   $stra = strip_tags($stra);

    return $stra;
  }

        $data = array();
        $this->NOW = date('Y-m-d H:i') . ':00';
        if( isset($this->request->get['keyword']) ) {

            $keywords = mb_strtolower(( $this->request->get['keyword'] ), 'UTF-8');

            if( ((mb_strlen(($keywords), 'UTF-8')) >= ($this->config->get('autosearch_symbol'))) AND ((($this->config->get('autosearch_status')) > 0)) ) {
                $parts = explode( ' ', $keywords );
                $add = '';

            if (($this->config->get('autosearch_asr_image')) <>'' ) {
                $image_width = $this->config->get('autosearch_asr_image');
                $image_height = $this->config->get('autosearch_asr_image');
            } else  { 
                $image_width = $this->config->get('config_image_cart_width');
                $image_height = $this->config->get('config_image_cart_height');
            }

                foreach( $parts as $part ) {
                    $add .= ' AND (LOWER(pd.name) LIKE "%' . $this->db->escape($part) . '%"';
                if (($this->config->get('autosearch_model')) > 0 ) $add .= ' OR LOWER(p.model) LIKE "%' . $this->db->escape($part) . '%"';
                if (($this->config->get('autosearch_sku')) > 0 ) $add .= ' OR LOWER(p.sku) LIKE "%' . $this->db->escape($part) . '%"';
                if (($this->config->get('autosearch_upc')) > 0 ) $add .= ' OR LOWER(p.upc) LIKE "%' . $this->db->escape($part) . '%"';
                if (($this->config->get('autosearch_ean')) > 0 ) $add .= ' OR LOWER(p.ean) LIKE "%' . $this->db->escape($part) . '%"';
                if (($this->config->get('autosearch_jan')) > 0 ) $add .= ' OR LOWER(p.jan) LIKE "%' . $this->db->escape($part) . '%"';
                if (($this->config->get('autosearch_isbn')) > 0 ) $add .= ' OR LOWER(p.isbn) LIKE "%' . $this->db->escape($part) . '%"';
                if (($this->config->get('autosearch_mpn')) > 0 ) $add .= ' OR LOWER(p.mpn) LIKE "%' . $this->db->escape($part) . '%"';
                if (($this->config->get('autosearch_location')) > 0 ) $add .= ' OR LOWER(p.location) LIKE "%' . $this->db->escape($part) . '%"';
                if (($this->config->get('autosearch_tag')) > 0 ) $add .= ' OR LOWER(pd.tag) LIKE "%' . $this->db->escape($part) . '%"';
                if (($this->config->get('autosearch_manufacturer')) > 0 ) $add .= ' OR LOWER(m.name) LIKE "%' . $this->db->escape($part) . '%"';
                if (($this->config->get('autosearch_attr')) > 0 ) $add .= ' OR LOWER(pa.text) LIKE "%' . $this->db->escape($part) . '%"';

                    $add .= ')';
                }
                $add = substr( $add, 4 );

        if ($this->customer->isLogged()) {
            $customer_group_id = $this->customer->getGroupId();
        } else {
            $customer_group_id = $this->config->get('config_customer_group_id');
        }

$sql = "SELECT DISTINCT *, ";

if (($this->config->get('autosearch_attr')) > 0 ) $sql .= " pa.text, ";

$sql .= " pd.product_id, 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)$customer_group_id . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < '" . $this->NOW . "') AND (pd2.date_end = '0000-00-00' OR pd2.date_end > '" . $this->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)$customer_group_id . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < '" . $this->NOW . "') AND (ps.date_end = '0000-00-00' OR ps.date_end > '" . $this->NOW . "')) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special, (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, 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) ";

if (($this->config->get('autosearch_attr')) > 0 ) $sql .= "LEFT JOIN " . DB_PREFIX . "product_attribute pa ON (p.product_id = pa.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 .= ' GROUP BY p.product_id ';

                if (($this->config->get('autosearch_sort')) > 0 ) {
                $sql .= ' ORDER BY LOWER(pd.name) ASC';
                } else {
                $sql .= ' ORDER BY p.date_available DESC, LOWER(pd.name) ASC';
                }

                $sql .= ' LIMIT ' . (int)$this->config->get('autosearch_limit');
                $res = $this->db->query( $sql );
                if( $res ) {
                    $data1 = ( isset($res->rows) ) ? $res->rows : $res->row;

                    $this->language->load('product/product');
                    $this->language->load('module/autosearch');
                    $this->load->model('tool/image');
                    $this->load->model('catalog/product');

                    $basehref = 'product/product&product_id=';

                    foreach( $data1 as $key => $values ) {
                        $data[$key]['href'] = htmlspecialchars_decode($this->url->link(get_sum ($basehref . $values['product_id'])), ENT_QUOTES);

                    $code = $values['name'];
                    if (($this->config->get('autosearch_codepage')) == 1 ) $code = mb_convert_encoding($code, 'utf-8', mb_detect_encoding($code));
                    if (($this->config->get('autosearch_codepage')) == 2 ) $code = mb_convert_encoding($code, 'utf-8');
                    $data[$key]['name'] = htmlspecialchars_decode( (get_sum ($code)), ENT_QUOTES);

                    if (($this->config->get('autosearch_show')) > 0 ) {
                        if (($values['image'] != '') && is_file(DIR_IMAGE . $values['image'])) {
                            $data[$key]['thumb'] = $this->model_tool_image->resize($values['image'], $image_width, $image_height);
                        } else {
                            $data[$key]['thumb'] = $this->model_tool_image->resize('no_image_latest.jpg', $image_width, $image_height);
                        }

                    } else {
                    $data[$key]['thumb'] = '';  
                    }

                    if (($this->config->get('autosearch_show_model')) > 0 ) {

                    $code = $values['model'];
                    if (($this->config->get('autosearch_codepage')) == 1 ) $code = mb_convert_encoding($code, 'utf-8', mb_detect_encoding($code));
                    if (($this->config->get('autosearch_codepage')) == 2 ) $code = mb_convert_encoding($code, 'utf-8');
                    $data[$key]['model'] = htmlspecialchars_decode( (get_sum ($code)), ENT_QUOTES);

                    } else {
                    $data[$key]['model'] = '';  
                    }

                    if (($this->config->get('autosearch_show_quantity')) > 0 ) {                    
                        if (($values['quantity']) > 0 ) {
                            if ($this->config->get('config_stock_display')) { 
                                $data[$key]['stock'] = ($this->language->get('text_stock')) . ' ' . ($values['quantity']);
                            } else {
                                $data[$key]['stock'] = $this->language->get('text_instock');
                            }
                        } else {
                        $data[$key]['stock'] = ($values['stock_status']);
                        }
                    } else {
                        $data[$key]['stock'] = '';  
                    }

                    if ((($this->config->get('autosearch_show_price')) > 0 ) and (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price'))){

                    if (VERSION >= '2.2') {
                        if ((float)$values['special']) {
                        $data[$key]['special'] = strip_tags(html_entity_decode($this->currency->format($this->tax->calculate($values['price'], $values['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']) , ENT_QUOTES, 'UTF-8'));
                        $data[$key]['price'] = strip_tags(html_entity_decode($this->currency->format($this->tax->calculate($values['special'], $values['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']) , ENT_QUOTES, 'UTF-8'));
                        } else {
                        $data[$key]['special'] = '';
                        $data[$key]['price'] = strip_tags(html_entity_decode($this->currency->format($this->tax->calculate($values['price'], $values['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']) , ENT_QUOTES, 'UTF-8'));
                        }   
                    } else {
                        if ((float)$values['special']) {
                        $data[$key]['special'] = strip_tags(html_entity_decode($this->currency->format($this->tax->calculate($values['price'], $values['tax_class_id'], $this->config->get('config_tax'))) , ENT_QUOTES, 'UTF-8'));
                        $data[$key]['price'] = strip_tags(html_entity_decode($this->currency->format($this->tax->calculate($values['special'], $values['tax_class_id'], $this->config->get('config_tax'))) , ENT_QUOTES, 'UTF-8'));
                        } else {
                        $data[$key]['special'] = '';
                        $data[$key]['price'] = strip_tags(html_entity_decode($this->currency->format($this->tax->calculate($values['price'], $values['tax_class_id'], $this->config->get('config_tax'))) , ENT_QUOTES, 'UTF-8'));
                        }
                    }

                    } else {
                    $data[$key]['price'] = '';
                    }

                    if (($this->config->get('autosearch_viewall')) != 'no' ) {
                    $data[$key]['viewall'] = $this->language->get('text_viewall');
                    } else {
                    $data[$key]['viewall'] = '';
                    }

                    } // конец массива
                }
            }
        }
        echo json_encode( $data );
    }
}
?>

此外,autosearch.js看起来像这样:

   //<![CDATA[

function doLiveSearch( ev, keywords ) {

    if( ev.keyCode == 38 || ev.keyCode == 40 ) {
        return false;
    }   

    $('#autosearch_search_results').remove();
    updown = -1;

    if( keywords == '' || keywords.length < 2 ) {
        return false;
    }
    keywords = encodeURI(keywords);

    $.ajax({url: $('base').attr('href') + 'index.php?route=module/autosearch/ajax_asr&keyword=' + keywords, dataType: 'json', success: function(result) {
        if( result.length > 0 ) {
            var eList = document.createElement('ul');
            eList.id = 'autosearch_search_results';
            var eListElem;
            var eLink;
            var eImage;

            for( var i in result ) {
                eListElem = document.createElement('li');
                eLink = document.createElement('a');

            if( (result[i].thumb) != '' )
            {
                eImage = document.createElement('img');
                eImage.src = result[i].thumb;
                eLink.appendChild(eImage);                  

            }
// name
var el_span = document.createElement('name');
    var textNode = document.createTextNode(result[i].name);
    eLink.appendChild(el_span);
    el_span.appendChild(textNode);




// model
            if( (result[i].model) != '' )
            {
var el_span = document.createElement('model');
    var textNode = document.createTextNode(result[i].model);
    eLink.appendChild(el_span);
    el_span.appendChild(textNode);
            }

                if( typeof(result[i].href) != 'undefined' ) {
                    eLink.href = result[i].href;
                }
                else {
                    eLink.href = $('base').attr('href') + 'index.php?route=product/product&product_id=' + result[i].product_id + '&keyword=' + keywords;
                }
                eListElem.appendChild(eLink);

            if( (result[i].price) != '' )
            {

var br = document.createElement("br");
eLink.appendChild(br);

// special price
    if( (result[i].special) != '' )
        {

var el_span = document.createElement('special-price');
    var textNode = document.createTextNode(result[i].special);
    eLink.appendChild(el_span);
    el_span.appendChild(textNode);
        }

// price
var el_span = document.createElement('price');
    var textNode = document.createTextNode(result[i].price);
    eLink.appendChild(el_span);
    el_span.appendChild(textNode);
            }

// quantity/stock
            if( (result[i].stock) != '' )
            {

var br = document.createElement("br");
eLink.appendChild(br);
eLink.appendChild( document.createTextNode(result[i].stock) );
            }

                eList.appendChild(eListElem);
            }
            if( $('#autosearch_search_results').length > 0 ) {
                $('#autosearch_search_results').remove();
            }

            //view all results
            if( (result[i].viewall) != '' )
            {
            eListElem = document.createElement('li');
            eLink = document.createElement('a');
                var el_span = document.createElement('viewall');
                var textNode = document.createTextNode(result[i].viewall);
                eLink.appendChild(el_span);
                el_span.appendChild(textNode);
                eLink.href = $('base').attr('href') + 'index.php?route=product/search&search=' + keywords;
            eListElem.appendChild(eLink);
            eList.appendChild(eListElem);
            }

            $('#search').append(eList);
        }
    }});

    return true;
}
function highlightQuery(string,searchQuery){
    if(!string){
        return "";
    }
    var expr = searchQuery;
    expr = expr.replace(/\s+/, "|",searchQuery);
    var regex = new RegExp(expr,"gi");
    return string.replace(regex, function($1){
        return '<span class="highlight">'+ $1 +'</span>';
    });
}
function upDownEvent( ev ) {
    var elem = document.getElementById('autosearch_search_results');
    var fkey = $('#search').find('[name=search]').first();


    if( elem ) {
        var length = elem.childNodes.length - 1;

        if( updown != -1 && typeof(elem.childNodes[updown]) != 'undefined' ) {
            $(elem.childNodes[updown]).removeClass('highlighted');
        }

        // Up
        if( ev.keyCode == 38 ) {
            updown = ( updown > 0 ) ? --updown : updown;
        }
        else if( ev.keyCode == 40 ) {
            updown = ( updown < length ) ? ++updown : updown;
        }

        if( updown >= 0 && updown <= length ) {
            $(elem.childNodes[updown]).addClass('highlighted');

            var text = elem.childNodes[updown].childNodes[0].text;
            if( typeof(text) == 'undefined' ) {
                text = elem.childNodes[updown].childNodes[0].innerText;
            }

        }
    }

    return false;
}

var updown = -1;

$(document).ready(function(){
    $('#search').find('[name=search]').attr('autocomplete', 'off'); //disable autocomplete

    $('#search').find('[name=search]').first().keyup(function(ev){
        doLiveSearch(ev, this.value);
    }).focus(function(ev){
        doLiveSearch(ev, this.value);
    }).keydown(function(ev){
        upDownEvent( ev );
    }).blur(function(){
        window.setTimeout("$('#autosearch_search_results').remove();updown=0;", 1500);
    });
    $(document).bind('keydown', function(ev) {
        try {
            if( ev.keyCode == 13 && $('.highlighted').length > 0 ) {
                document.location.href = $('.highlighted').find('a').first().attr('href');
            }
        }
        catch(e) {}
    });
});
//]]>

我做错了什么?我真的迷路了。提前谢谢大家。

1 个答案:

答案 0 :(得分:0)

我认为问题出在您的查询中。制造商名称位于 manufacturer_description 表中。因此,您需要加入 manufacturer_description 表,而不是 manufacturer 表。另请检查制造商的语言ID。

将查询更改为:

$sql .= " pd.product_id, 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)$customer_group_id . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < '" . $this->NOW . "') AND (pd2.date_end = '0000-00-00' OR pd2.date_end > '" . $this->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)$customer_group_id . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < '" . $this->NOW . "') AND (ps.date_end = '0000-00-00' OR ps.date_end > '" . $this->NOW . "')) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special, (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, 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_description m ON (p.manufacturer_id = m.manufacturer_id) AND m.language_id = '" . (int)$this->config->get('config_language_id') . "' ";