Merge pull request #3078 from MattApril/product_sorting

Products: Fix for missing or invalid sort direction
This commit is contained in:
Jitendra Singh 2020-05-18 20:59:47 +05:30 committed by GitHub
commit ff49583f81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 8 deletions

View File

@ -83,8 +83,9 @@ class Toolbar extends AbstractProduct
public function isOrderCurrent($key)
{
$params = request()->input();
$orderDirection = $params['order'] ?? 'asc';
if (isset($params['sort']) && $key == $params['sort'] . '-' . $params['order']) {
if (isset($params['sort']) && $key == $params['sort'] . '-' . $orderDirection) {
return true;
} elseif (! isset($params['sort'])) {
$sortBy = core()->getConfigData('catalog.products.storefront.sort_by')

View File

@ -140,18 +140,20 @@ class ProductRepository extends Repository
if (isset($params['search']))
$qb->where('product_flat.name', 'like', '%' . urldecode($params['search']) . '%');
# sort direction
$orderDirection = 'asc';
if( isset($params['order']) && in_array($params['order'], ['desc', 'asc']) ){
$orderDirection = $params['order'];
}
if (isset($params['sort'])) {
$attribute = $this->attributeRepository->findOneByField('code', $params['sort']);
if ($attribute) {
if ($params['sort'] == 'price') {
if ($attribute->code == 'price') {
$qb->orderBy('min_price', $params['order']);
} else {
$qb->orderBy($attribute->code, $params['order']);
}
if ($attribute->code == 'price') {
$qb->orderBy('min_price', $orderDirection);
} else {
$qb->orderBy($params['sort'] == 'created_at' ? 'product_flat.created_at' : $attribute->code, $params['order']);
$qb->orderBy($params['sort'] == 'created_at' ? 'product_flat.created_at' : $attribute->code, $orderDirection);
}
}
}