Add search sorting
This commit is contained in:
parent
ef2ba29d0a
commit
95be1bb321
|
|
@ -166,51 +166,6 @@ class ProductRepository extends WProductRepository
|
|||
}
|
||||
}
|
||||
|
||||
$attributeFilters = $this->attributeRepository
|
||||
->getProductDefaultAttributes(array_keys(
|
||||
request()->except(['price'])
|
||||
));
|
||||
|
||||
if (count($attributeFilters) > 0) {
|
||||
$query->where(function ($filterQuery) use ($attributeFilters) {
|
||||
|
||||
foreach ($attributeFilters as $attribute) {
|
||||
$filterQuery->orWhere(function ($attributeQuery) use ($attribute) {
|
||||
|
||||
$column = DB::getTablePrefix() . 'product_attribute_values.' . ProductAttributeValueProxy::modelClass()::$attributeTypeFields[$attribute->type];
|
||||
|
||||
$filterInputValues = explode(',', request()->get($attribute->code));
|
||||
|
||||
# define the attribute we are filtering
|
||||
$attributeQuery = $attributeQuery->where('product_attribute_values.attribute_id', $attribute->id);
|
||||
|
||||
# apply the filter values to the correct column for this type of attribute.
|
||||
if ($attribute->type != 'price') {
|
||||
|
||||
$attributeQuery->where(function ($attributeValueQuery) use ($column, $filterInputValues) {
|
||||
foreach ($filterInputValues as $filterValue) {
|
||||
if (! is_numeric($filterValue)) {
|
||||
continue;
|
||||
}
|
||||
$attributeValueQuery->orWhereRaw("find_in_set(?, {$column})", [$filterValue]);
|
||||
}
|
||||
});
|
||||
|
||||
} else {
|
||||
$attributeQuery->where($column, '>=', core()->convertToBasePrice(current($filterInputValues)))
|
||||
->where($column, '<=', core()->convertToBasePrice(end($filterInputValues)));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
# this is key! if a product has been filtered down to the same number of attributes that we filtered on,
|
||||
# we know that it has matched all of the requested filters.
|
||||
$query->groupBy('variants.id');
|
||||
$query->havingRaw('COUNT(*) = ' . count($attributeFilters));
|
||||
}
|
||||
|
||||
return $query->groupBy('product_flat.id');
|
||||
// ->orderBy('product_id', 'desc');
|
||||
})->paginate($perPage);
|
||||
|
|
|
|||
Loading…
Reference in New Issue