使用搜索输入字段时,如何显示某个类别的所有产品?
e.g。搜索栏:“酸奶” - >显示酸奶类别中的所有产品。
答案 0 :(得分:1)
我以这种方式覆盖了Search类和find方法:
你可以看到我在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);
}