ps_facelet_search/src/adapter/Mysql.php
foreach ($this->getFilters() as $filterName => $filterContent) {
$selectAlias = 'p';
if (array_key_exists($filterName, $filterToTableMapping)) {
$joinMapping = $filterToTableMapping[$filterName];
$selectAlias = $joinMapping['tableAlias'];
$filterName = isset($joinMapping['fieldName']) ? $joinMapping['fieldName'] : $filterName;
}
foreach ($filterContent as $operator => $values) {
if (count($values) == 1) {
$values = current($values);
if ($operator === '=') {
if (count($values) == 1) {
$whereConditions[] =
$selectAlias . '.' . $filterName . $operator . "'" . current($values) . "'";
} else {
$whereConditions[] =
$selectAlias . '.' . $filterName . ' IN (' . $this->getJoinedEscapedValue(', ', $values) . ')';
}
} else {
$orConditions = [];
foreach ($values as $value) {
$orConditions[] = $selectAlias . '.' . $filterName . $operator . $value;
}
$whereConditions[] = implode(' OR ', $orConditions);
}
//tcp
$whereConditions[] = 'sa.quantity > 0';
}
}
}
ps_facelet_search/src/filters/Products.php
public function getProductByFilters(
ProductSearchQuery $query,
array $selectedFilters = []
) {
// Load sorting type and direction, validate it and apply fallback if needed
$orderBy = $query->getSortOrder()->toLegacyOrderBy(false);
$orderWay = $query->getSortOrder()->toLegacyOrderWay();
$orderWay = Validate::isOrderWay($orderWay) ? $orderWay : 'ASC';
$orderBy = Validate::isOrderBy($orderBy) ? $orderBy : 'position';
// Apply it to the filter
$this->searchAdapter->setOrderField($orderBy);
$this->searchAdapter->setOrderDirection($orderWay);
// Add stock filter to ensure only products in stock are returned
$this->searchAdapter->addOperationsFilter(
'in_stock_filter',
[
[
['quantity', [0], '>'], // Only include products with quantity > 0
],
]
);
$this->searchAdapter->addGroupBy('id_product');
if (isset($selectedFilters['price']) || $orderBy === 'price') {
$this->searchAdapter->addSelectField('id_product');
$this->searchAdapter->addSelectField('price');
$this->searchAdapter->addSelectField('price_min');
$this->searchAdapter->addSelectField('price_max');
}
// Get full list of matching products
$fullProductList = $this->searchAdapter->execute();
// Count them
$totalProductCount = count($fullProductList);
// Get pagination
$productsPerPage = (int) $query->getResultsPerPage();
$page = (int) $query->getPage();
// Cut them down by pagination
$finalProductList = array_slice(
$fullProductList,
($page - 1) * $productsPerPage,
$productsPerPage
);
// And run post filter
$this->pricePostFiltering($finalProductList, $selectedFilters);
return [
'products' => $finalProductList,
'count' => $totalProductCount,
];
}
ps_facelet_search/src/filters/block.php
private function getAttributesBlock($filter, $selectedFilters, $idLang)
{
$attributesBlock = [];
$filteredSearchAdapter = null;
$idAttributeGroup = $filter['id_value'];
if (!empty($selectedFilters['id_attribute_group'])) {
foreach ($selectedFilters['id_attribute_group'] as $key => $selectedFilter) {
if ($key == $idAttributeGroup) {
$filteredSearchAdapter = $this->searchAdapter->getFilteredSearchAdapter('with_attributes_' . $idAttributeGroup);
break;
}
}
}
if (!$filteredSearchAdapter) {
$filteredSearchAdapter = $this->searchAdapter->getFilteredSearchAdapter();
}
// Add a global filter to include only in-stock products
$filteredSearchAdapter->addOperationsFilter(
'in_stock_filter',
[
[
['quantity', [0], '>'], // Filter only products with quantity > 0
],
]
);
$attributesGroup = $this->dataAccessor->getAttributesGroups($idLang);
if ($attributesGroup === []) {
return $attributesBlock;
}
$attributes = $this->dataAccessor->getAttributes($idLang, $idAttributeGroup);
$filteredSearchAdapter->addOperationsFilter(
'id_attribute_group_' . $idAttributeGroup,
[[['id_attribute_group', [(int) $idAttributeGroup]]]]
);
$results = $filteredSearchAdapter->valueCount('id_attribute');
foreach ($results as $key => $values) {
$idAttribute = $values['id_attribute'];
if (!isset($attributes[$idAttribute])) {
continue;
}
$count = $values['c'];
$attribute = $attributes[$idAttribute];
$idAttributeGroup = $attribute['id_attribute_group'];
if (!isset($attributesBlock[$idAttributeGroup])) {
$attributeGroup = $attributesGroup[$idAttributeGroup];
$attributesBlock[$idAttributeGroup] = [
'type_lite' => 'id_attribute_group',
'type' => 'id_attribute_group',
'id_key' => $idAttributeGroup,
'name' => $attributeGroup['attribute_group_name'],
'is_color_group' => (bool) $attributeGroup['is_color_group'],
'values' => [],
'url_name' => $attributeGroup['url_name'],
'meta_title' => $attributeGroup['meta_title'],
'filter_show_limit' => (int) $filter['filter_show_limit'],
'filter_type' => $filter['filter_type'],
];
}
$attributesBlock[$idAttributeGroup]['values'][$idAttribute] = [
'name' => $attribute['name'],
'nbr' => $count,
'url_name' => $attribute['url_name'],
'meta_title' => $attribute['meta_title'],
];
if ($attributesBlock[$idAttributeGroup]['is_color_group'] !== false) {
$attributesBlock[$idAttributeGroup]['values'][$idAttribute]['color'] = $attribute['color'];
}
if (array_key_exists('id_attribute_group', $selectedFilters)) {
foreach ($selectedFilters['id_attribute_group'] as $selectedAttribute) {
if (in_array($idAttribute, $selectedAttribute)) {
$attributesBlock[$idAttributeGroup]['values'][$idAttribute]['checked'] = true;
}
}
}
}
foreach ($attributesBlock as $idAttributeGroup => $value) {
$attributesBlock[$idAttributeGroup]['values'] = $this->sortByKey($attributes, $value['values']);
}
$attributesBlock = $this->sortByKey($attributesGroup, $attributesBlock);
return $attributesBlock;
}