Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make backport return items ordered by position #1021

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 40 additions & 23 deletions src/Product/CoreSearchBackport.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ public function get80($expr)
$context = Context::getContext();
$db = Db::getInstance(_PS_USE_SQL_SLAVE_);

$scoreArray = [];
$fuzzyLoop = 0;
$wordCnt = 0;
$eligibleProducts2Full = [];
Expand All @@ -342,15 +343,15 @@ public function get80($expr)

$sql_param_search = Search::getSearchParamFromWord($word);
$sql = 'SELECT DISTINCT si.id_product ' .
'FROM ' . _DB_PREFIX_ . 'search_word sw ' .
'LEFT JOIN ' . _DB_PREFIX_ . 'search_index si ON sw.id_word = si.id_word ' .
'LEFT JOIN ' . _DB_PREFIX_ . 'product_shop product_shop ON (product_shop.`id_product` = si.`id_product`) ' .
'WHERE sw.id_lang = ' . (int) $context->language->id . ' ' .
'AND sw.id_shop = ' . $context->shop->id . ' ' .
'AND product_shop.`active` = 1 ' .
'AND product_shop.`visibility` IN ("both", "search") ' .
'AND product_shop.indexed = 1 ' .
'AND sw.word LIKE ';
'FROM ' . _DB_PREFIX_ . 'search_word sw ' .
'LEFT JOIN ' . _DB_PREFIX_ . 'search_index si ON sw.id_word = si.id_word ' .
'LEFT JOIN ' . _DB_PREFIX_ . 'product_shop product_shop ON (product_shop.`id_product` = si.`id_product`) ' .
'WHERE sw.id_lang = ' . (int) $context->language->id . ' ' .
'AND sw.id_shop = ' . $context->shop->id . ' ' .
'AND product_shop.`active` = 1 ' .
'AND product_shop.`visibility` IN ("both", "search") ' .
'AND product_shop.indexed = 1 ' .
'AND sw.word LIKE ';

while (!($result = $db->executeS($sql . "'" . $sql_param_search . "';", true, false))) {
if (
Expand All @@ -373,6 +374,8 @@ public function get80($expr)
} else {
$eligibleProducts2 = array_intersect($eligibleProducts2, $productIds);
}

$scoreArray[] = 'sw.word LIKE \'' . $sql_param_search . '\'';
}
$wordCnt += count($words);
if ($eligibleProducts2) {
Expand All @@ -386,27 +389,41 @@ public function get80($expr)
return [];
}

$sqlScore = '';
if (!empty($scoreArray) && is_array($scoreArray)) {
$sqlScore = ',( ' .
'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) $context->language->id . ' ' .
'AND sw.id_shop = ' . $context->shop->id . ' ' .
'AND si.id_product = p.id_product ' .
'AND (' . implode(' OR ', $scoreArray) . ') ' .
') position';
}

$sqlGroups = '';
if (Group::isFeatureActive()) {
$groups = FrontController::getCurrentCustomerGroups();
$sqlGroups = 'AND cg.`id_group` ' . (count($groups) ? 'IN (' . implode(',', $groups) . ')' : '=' . (int) Group::getCurrent()->id);
}

$results = $db->executeS(
'SELECT DISTINCT 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 ' .
'AND cp.id_product IN (' . implode(',', $eligibleProducts2Full) . ')' . $sqlGroups,
true,
false
);
'SELECT DISTINCT cp.`id_product` ' . $sqlScore . ' ' .
'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 ' .
'AND cp.id_product IN (' . implode(',', $eligibleProducts2Full) . ')' . $sqlGroups . '
ORDER BY position DESC, p.id_product ASC',
true,
false
);

return array_column($results, 'id_product');
}
Expand Down
Loading