Merge pull request #3549 from devansh-webkul/sort_by_configuration_issue

Fixed sort by configuration from admin end is not working properly #3535
This commit is contained in:
Jitendra Singh 2020-07-22 20:58:58 +05:30 committed by GitHub
commit 951ac00df2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 10 deletions

View File

@ -151,18 +151,14 @@ class ProductRepository extends Repository
$orderDirection = 'asc';
if( isset($params['order']) && in_array($params['order'], ['desc', 'asc']) ){
$orderDirection = $params['order'];
} else {
$orderDirection = $this->getDefaultSortByOption()[1];
}
if (isset($params['sort'])) {
$attribute = $this->attributeRepository->findOneByField('code', $params['sort']);
if ($attribute) {
if ($attribute->code == 'price') {
$qb->orderBy('min_price', $orderDirection);
} else {
$qb->orderBy($params['sort'] == 'created_at' ? 'product_flat.created_at' : $attribute->code, $orderDirection);
}
}
$this->checkSortAttributeAndGenerateQuery($qb, $params['sort'], $orderDirection);
} else {
$this->checkSortAttributeAndGenerateQuery($qb, $this->getDefaultSortByOption()[0], $orderDirection);
}
if ( $priceFilter = request('price') ){
@ -453,4 +449,38 @@ class ProductRepository extends Repository
->orderBy('product_id', 'desc');
})->get();
}
/**
* Get default sort by option
*
* @return array
*/
private function getDefaultSortByOption()
{
$config = core()->getConfigData('catalog.products.storefront.sort_by');
return explode('-', $config);
}
/**
* Check sort attribute and generate query
*
* @param object $query
* @param string $sort
* @param string $direction
* @return object
*/
private function checkSortAttributeAndGenerateQuery($query, $sort, $direction)
{
$attribute = $this->attributeRepository->findOneByField('code', $sort);
if ($attribute) {
if ($attribute->code == 'price') {
$query->orderBy('min_price', $direction);
} else {
$query->orderBy($sort == 'created_at' ? 'product_flat.created_at' : $attribute->code, $direction);
}
}
return $query;
}
}

View File

@ -399,7 +399,7 @@ class Configurable extends AbstractType
{
return '<span class="price-label">' . trans('shop::app.products.price-label') . '</span>'
. ' '
. '<span class="final-price">' . core()->currency($this->getMinimalPrice()) . '</span>';
. '<span class="final-price">' . core()->currency($this->product->price) . '</span>';
}
/**