Prestashop 如何按类别搜索显示产品?

Prestashop 如何按类别搜索显示产品?,prestashop,prestashop-1.6,Prestashop,Prestashop 1.6,使用搜索输入字段时,如何显示一个类别的所有产品 e、 g.搜索字段:“酸奶”-->显示酸奶类别中的所有产品。我以这种方式覆盖搜索类和查找方法: 正如您所看到的,我在find方法($filterCats)中添加了一个参数,这是一个带有要筛选的类别id的数组,然后对该数组进行内爆,并在查询中使用该字符串 public static function find($id_lang, $expr, $page_number = 1, $page_size = 1, $order_by = 'pos

使用搜索输入字段时,如何显示一个类别的所有产品


e、 g.搜索字段:“酸奶”-->显示酸奶类别中的所有产品。

我以这种方式覆盖搜索类和查找方法:

正如您所看到的,我在find方法($filterCats)中添加了一个参数,这是一个带有要筛选的类别id的数组,然后对该数组进行内爆,并在查询中使用该字符串

    public static function find($id_lang, $expr, $page_number = 1, $page_size = 1, $order_by = 'position',
    $order_way = 'desc', $ajax = false, $use_cookie = true, Context $context = null, $filterCats)
{

    $activeFilterString = implode($filterCats, ',');


    if (!$context)
        $context = Context::getContext();
    $db = Db::getInstance(_PS_USE_SQL_SLAVE_);


    if ($page_number < 1) $page_number = 1;
    if ($page_size < 1) $page_size = 1;

    if (!Validate::isOrderBy($order_by) || !Validate::isOrderWay($order_way))
        return false;

    $intersect_array = array();
    $score_array = array();
    $words = explode(' ', Search::sanitize($expr, $id_lang, false, $context->language->iso_code));

    foreach ($words as $key => $word)
        if (!empty($word) && strlen($word) >= (int)Configuration::get('PS_SEARCH_MINWORDLEN'))
        {
            $word = str_replace('%', '\\%', $word);
            $word = str_replace('_', '\\_', $word);
            $intersect_array[] = 'SELECT si.id_product
                FROM '._DB_PREFIX_.'search_word sw
                LEFT JOIN '._DB_PREFIX_.'search_index si ON sw.id_word = si.id_word
                WHERE sw.id_lang = '.(int)$id_lang.'
                    AND sw.id_shop = '.$context->shop->id.'
                    AND sw.word LIKE
                '.($word[0] == '-'
                    ? ' \''.pSQL(Tools::substr($word, 1, PS_SEARCH_MAX_WORD_LENGTH)).'%\''
                    : '\''.pSQL(Tools::substr($word, 0, PS_SEARCH_MAX_WORD_LENGTH)).'%\''
                );

            if ($word[0] != '-')
                $score_array[] = 'sw.word LIKE \''.pSQL(Tools::substr($word, 0, PS_SEARCH_MAX_WORD_LENGTH)).'%\'';
        }
        else
            unset($words[$key]);

    if (!count($words))
        return ($ajax ? array() : array('total' => 0, 'result' => array()));

    $score = '';
    if (count($score_array))
        $score = ',(
            SELECT SUM(weight)
            FROM '._DB_PREFIX_.'search_word sw
            LEFT JOIN '._DB_PREFIX_.'search_index si ON sw.id_word = si.id_word
            WHERE sw.id_lang = '.(int)$id_lang.'
                AND sw.id_shop = '.$context->shop->id.'
                AND si.id_product = p.id_product
                AND ('.implode(' OR ', $score_array).')
        ) position';

    $sql_groups = '';
    if (Group::isFeatureActive())
    {
        $groups = FrontController::getCurrentCustomerGroups();
        $sql_groups = 'AND cg.`id_group` '.(count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
    }

    if (count($fitersArray) == 0){

        $results = $db->executeS('
        SELECT cp.`id_product`
        FROM `'._DB_PREFIX_.'category_product` cp
        '.(Group::isFeatureActive() ? 'INNER JOIN `'._DB_PREFIX_.'category_group` cg ON cp.`id_category` = cg.`id_category`' : '').'
        INNER JOIN `'._DB_PREFIX_.'category` c ON cp.`id_category` = c.`id_category`
        INNER JOIN `'._DB_PREFIX_.'product` p ON cp.`id_product` = p.`id_product`
        '.Shop::addSqlAssociation('product', 'p', false).'
        WHERE c.`active` = 1
        AND product_shop.`active` = 1
        AND product_shop.`visibility` IN ("both", "search")
        AND product_shop.indexed = 1
        '.$sql_groups);

    } else if (count($fitersArray) > 0) {

        $results = $db->executeS('
        SELECT cp.`id_product`
        FROM `'._DB_PREFIX_.'category_product` cp
        '.(Group::isFeatureActive() ? 'INNER JOIN `'._DB_PREFIX_.'category_group` cg ON cp.`id_category` = cg.`id_category`' : '').'
        INNER JOIN `'._DB_PREFIX_.'category` c ON cp.`id_category` = c.`id_category`
        INNER JOIN `'._DB_PREFIX_.'product` p ON cp.`id_product` = p.`id_product`
        '.Shop::addSqlAssociation('product', 'p', false).'
        WHERE c.`active` = 1
        AND product_shop.`active` = 1
        AND product_shop.`visibility` IN ("both", "search")
        AND c.`id_category` IN (' . $activeFilterString .')
        AND product_shop.indexed = 1
        '.$sql_groups);

    }
公共静态函数find($id\u lang,$expr,$page\u number=1,$page\u size=1,$order\u by='position',
$order\u way='desc',$ajax=false,$use\u cookie=true,Context$Context=null,$filterCats)
{
$activeFilterString=内爆($filterCats,,');
如果(!$context)
$context=context::getContext();
$db=db::getInstance(\u PS\u USE\u SQL\u SLAVE\u);
如果($页码<1)$页码=1;
如果($page_size<1)$page_size=1;
如果(!Validate::isOrderBy($order_-by)| |!Validate::isOrderWay($order_-way))
返回false;
$intersect_数组=数组();
$score_array=array();
$words=explode(“”,Search::sanitize($expr,$id_lang,false,$context->language->iso_code));
foreach($words作为$key=>$word)
如果(!empty($word)&&strlen($word)>=(int)配置::get('PS\u SEARCH\u MINWORDLEN'))
{
$word=str\u replace(“%”、“\\%”、$word);
$word=str\u replace(“\”、“\ \ \”、$word);
$intersect_数组[]='选择si.id_产品
从“.\u DB\u前缀\u搜索\u单词sw”
左连接“.\u DB\u PREFIX\u.”搜索\u索引si ON sw.id\u word=si.id\u word
其中sw.id_lang='(int)$id_lang'
和sw.id_shop='.$context->shop->id.'
和sw.wordlike
“.($word[0]==”-'
pSQL(工具::substr($word,1,PS\u SEARCH\u MAX\u word\u LENGTH))。%\'
:'\''.pSQL(工具::substr($word,0,PS\u SEARCH\u MAX\u word\u LENGTH))。%\''
);
如果($word[0]!='-'))
$score\u数组[]='sw.word LIKE\''.pSQL(工具::substr($word,0,PS\u SEARCH\u MAX\u word\u LENGTH))。%\';
}
其他的
未设置($words[$key]);
如果(!计数($words))
返回($ajax?array():array('total'=>0,'result'=>array());
$score='';
if(计数($score_数组))
$score='(
选择总和(权重)
从“.\u DB\u前缀\u搜索\u单词sw”
左连接“.\u DB\u PREFIX\u.”搜索\u索引si ON sw.id\u word=si.id\u word
其中sw.id_lang='(int)$id_lang'
和sw.id_shop='.$context->shop->id.'
和si.id_乘积=p.id_乘积
和('.内爆('OR',$score_数组)。)
)位置';
$sql_组=“”;
如果(组::isFeatureActive())
{
$groups=FrontController::getCurrentCustomerGroups();
$sql_groups='和cg.'id_groups`'(count($groups)?'IN('.infrade(',',$groups)。'):'=1');
}
如果(计数($fitersArray)==0){
$results=$db->executeS('
选择cp.`id\u产品`
来自`.\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
“。(Group::isFeatureActive()?”内部联接“。”DB\U前缀“.category\U Group`cg ON cp.`id\U category`=cg.`id\U category`:“”)。”
cp上的内部联接“.”DB\u前缀“.”category`c.`id\u category`=c.`id\u category`
cp上的内部联接“.”DB_前缀“.”product`p.`id_product`=p.`id_product`
'.Shop::addSqlAssociation('product','p',false)。'
其中c.`active`=1
和产品商店。`active`=1
和product_shop.“两者”、“搜索”中的“可见性”
和product_shop.index=1
“.$sql_组);
}else if(计数($fitersArray)>0){
$results=$db->executeS('
选择cp.`id\u产品`
来自`.\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
“。(Group::isFeatureActive()?”内部联接“。”DB\U前缀“.category\U Group`cg ON cp.`id\U category`=cg.`id\U category`:“”)。”
cp上的内部联接“.”DB\u前缀“.”category`c.`id\u category`=c.`id\u category`
cp上的内部联接“.”DB_前缀“.”product`p.`id_product`=p.`id_product`
'.Shop::addSqlAssociation('product','p',false)。'
其中c.`active`=1
和产品商店。`active`=1
和product_shop.“两者”、“搜索”中的“可见性”
和('.$activeFilterString.'中的'id_category')
和product_shop.index=1
“.$sql_组);
}

为什么有人会降低这个问题的等级?没有类似的问题。这是直截了当的,附带了一致性和标签。毫无意义!如果你不想帮忙,那就不要,但不要再激怒需要提示和提示的人。他们真的应该给出否决投票的理由,因为有时候这会帮助人们理解为什么会被否决这样做是为了他们将来可以避免。即使我也无法理解你投反对票的原因。无论如何,在我的预选节目中,我从导航菜单中选择了类别,例如酸奶,它显示了该类别的所有产品。那么你有什么理由要这样做吗?我的意思是,你没有在菜单列表中保留类别吗?嗨@Shrilekha,我想这样做是因为,我有一些产品不是通过产品名称而不是类别名称来知道的。客人只关心类别。类别是他们想要获得许可产品的国家许可证。谢谢。你在什么文件中添加了这个?我想它属于blocksearch模块,不是吗?嗨,你找不到他在classes/Search.php中的方法