diff --git a/packages/Webkul/Product/src/Repositories/ProductRepository.php b/packages/Webkul/Product/src/Repositories/ProductRepository.php index 690d6658d..f602d5608 100755 --- a/packages/Webkul/Product/src/Repositories/ProductRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductRepository.php @@ -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; + } } \ No newline at end of file diff --git a/packages/Webkul/Product/src/Type/Configurable.php b/packages/Webkul/Product/src/Type/Configurable.php index 393287536..5d108697a 100644 --- a/packages/Webkul/Product/src/Type/Configurable.php +++ b/packages/Webkul/Product/src/Type/Configurable.php @@ -399,7 +399,7 @@ class Configurable extends AbstractType { return '' . trans('shop::app.products.price-label') . '' . ' ' - . '' . core()->currency($this->getMinimalPrice()) . ''; + . '' . core()->currency($this->product->price) . ''; } /**