Skip to content

Commit

Permalink
Merge pull request #916 from Hlavtox/fix-empty
Browse files Browse the repository at this point in the history
Fix empty stock filter, add comments
  • Loading branch information
nicosomb authored Sep 8, 2023
2 parents 34e8c85 + 54b5307 commit 7a6993f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Adapter/InterfaceAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function addFilter($filterName, $values, $operator = '=');
public function addOperationsFilter($filterName, array $operations);

/**
* Add fieldName in the current search result
* Add fieldName in the current search result. If the field already exists, it's skipped.
*
* @param string $fieldName
*
Expand Down
24 changes: 19 additions & 5 deletions src/Adapter/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,26 @@ public function execute()
*/
public function getQuery()
{
// Prepare mapping for joined tables
$filterToTableMapping = $this->getFieldMapping();

// Process and generate all fields for the SQL query below
$orderField = $this->computeOrderByField($filterToTableMapping);

// Now, let's build the query...
// If this query IS the initial population (the base table), we are selecting from product table
if ($this->getInitialPopulation() === null) {
$referenceTable = _DB_PREFIX_ . 'product';
} else {
$referenceTable = '(' . $this->getInitialPopulation()->getQuery() . ')';
}

$query = 'SELECT ';

$selectFields = $this->computeSelectFields($filterToTableMapping);
$whereConditions = $this->computeWhereConditions($filterToTableMapping);
$joinConditions = $this->computeJoinConditions($filterToTableMapping);
$groupFields = $this->computeGroupByFields($filterToTableMapping);

$query .= implode(', ', $selectFields) . ' FROM ' . $referenceTable . ' p';
$query = 'SELECT ' . implode(', ', $selectFields) . ' FROM ' . $referenceTable . ' p';

foreach ($joinConditions as $joinAliasInfos) {
foreach ($joinAliasInfos as $tableAlias => $joinInfos) {
Expand Down Expand Up @@ -365,23 +368,26 @@ protected function computeOrderByField(array $filterToTableMapping)
{
$orderField = $this->getOrderField();

// If we have set an initial population, add this field into initial population selects
if ($this->getInitialPopulation() !== null && !empty($orderField)) {
$this->getInitialPopulation()->addSelectField($orderField);
}

// do not try to process the orderField if it already has an alias, or if it's a group function
// Do not try to process the orderField if it already has an alias, or if it's a group function
if (empty($orderField) || strpos($orderField, '.') !== false
|| strpos($orderField, '(') !== false) {
return $orderField;
}

// Alter order by field if it's a price column
if ($orderField === 'price') {
$orderField = $this->getOrderDirection() === 'asc' ? 'price_min' : 'price_max';
}

// Add table mapping or p. prefix depending on field type
$orderField = $this->computeFieldName($orderField, $filterToTableMapping, true);

// put some products at the end of the list
// Alter order by field and add some products to the end of the list, if required
$orderField = $this->computeShowLast($orderField, $filterToTableMapping);

return $orderField;
Expand Down Expand Up @@ -485,6 +491,7 @@ protected function computeFieldName($fieldName, $filterToTableMapping, $sortByFi
*/
protected function computeSelectFields(array $filterToTableMapping)
{
// Add already added select fields to current query
$selectFields = [];
foreach ($this->getSelectFields() as $key => $selectField) {
$selectFields[] = $this->computeFieldName($selectField, $filterToTableMapping);
Expand Down Expand Up @@ -784,8 +791,11 @@ public function valueCount($fieldName = null)
*/
public function useFiltersAsInitialPopulation()
{
// Initial population has NO LIMIT and no ORDER BY
$this->setLimit(null);
$this->setOrderField('');

// We add basic select fields we will need to matter what
$this->setSelectFields(
[
'id_product',
Expand All @@ -797,7 +807,11 @@ public function useFiltersAsInitialPopulation()
'sales',
]
);

// Clone it, add it to initial population
$this->initialPopulation = clone $this;

// Reset all filters so we start clean and add only the base select, we don't need anything else
$this->resetAll();
$this->addSelectField('id_product');
}
Expand Down
9 changes: 6 additions & 3 deletions src/Product/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,14 @@ public function initSearch($selectedFilters)
// Add filters that the user has selected for current query
$this->addSearchFilters($selectedFilters);

// Adds filters that specific for category page
// Adds filters that specific for this controller
$this->addControllerSpecificFilters();

// Add group by and flush it, let's go
// Add group by to remove duplicate values
$this->getSearchAdapter()->addGroupBy('id_product');

// Move the current search into the "initialPopulation"
// This initialPopulation will be used to generate the base table in the final query
$this->getSearchAdapter()->useFiltersAsInitialPopulation();
}

Expand Down Expand Up @@ -312,7 +315,7 @@ private function addControllerSpecificFilters()
{
// Category page
if ($this->query->getQueryType() == 'category') {
// If any category filter was user selected, we don't have anything to do here
// We check if some specific filter of this type wasn't added before by the customer
if (!empty($this->getSearchAdapter()->getFilter('id_category'))) {
return;
}
Expand Down
22 changes: 9 additions & 13 deletions src/Product/SearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,8 @@ private function addEncodedFacetsToFilters(array $facets)

/**
* Remove the facet when there's only 1 result.
* Keep facet status when it's a slider
* Keep facet status when it's a slider.
* Keep facet status if it's a availability facet.
*
* @param array $facets
* @param int $totalProducts
Expand All @@ -522,12 +523,6 @@ private function hideUselessFacets(array $facets, $totalProducts)
continue;
}

// We won't apply this to availability facet, let's keep the value displayed
// Don't worry, the facet will be hidden if there are no values with products
if ($facet->getType() == 'availability') {
continue;
}

// Now the rest of facets - we apply this logic
$totalFacetProducts = 0;
$usefulFiltersCount = 0;
Expand All @@ -538,21 +533,22 @@ private function hideUselessFacets(array $facets, $totalProducts)
}
}

// We display the facet in several cases
$facet->setDisplayed(
// There are two filters displayed
// If there are two filters available
$usefulFiltersCount > 1
||
/*
* There is only one fitler and the
* magnitude is different than the
* total products
*/
// There is only one filter available, but it furhter reduces the product selection
(
count($facet->getFilters()) === 1
&& $totalFacetProducts < $totalProducts
&& $usefulFiltersCount > 0
)
||
// If there is only one filter, but it's availability filter - we want this one to be displayed all the time
($usefulFiltersCount === 1 && $facet->getType() == 'availability')
);
// Other cases - hidden by default
}
}

Expand Down

0 comments on commit 7a6993f

Please sign in to comment.