|
@@ -92,10 +92,54 @@ class ProductGraphQLProvider implements ProviderInterface
|
|
|
$locale = $args['locale'] ?? request()->attributes->get('bagisto_locale');
|
|
$locale = $args['locale'] ?? request()->attributes->get('bagisto_locale');
|
|
|
$channel = $args['channel'] ?? request()->attributes->get('bagisto_channel');
|
|
$channel = $args['channel'] ?? request()->attributes->get('bagisto_channel');
|
|
|
|
|
|
|
|
|
|
+ // Pre-parse the filter to expose category_id for the FEATURED sort,
|
|
|
|
|
+ // which must join product_categories scoped to the current category.
|
|
|
|
|
+ $sortCategoryId = null;
|
|
|
|
|
+ if (! empty($args['filter'])) {
|
|
|
|
|
+ $preFilter = is_string($args['filter'])
|
|
|
|
|
+ ? json_decode($args['filter'], true)
|
|
|
|
|
+ : $args['filter'];
|
|
|
|
|
+
|
|
|
|
|
+ if (is_array($preFilter) && ! empty($preFilter['category_id'])) {
|
|
|
|
|
+ $sortCategoryId = (int) $preFilter['category_id'];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if (! empty($elasticSearchIds) && ! isset($args['sortKey'])) {
|
|
if (! empty($elasticSearchIds) && ! isset($args['sortKey'])) {
|
|
|
$query->orderByRaw('FIELD(products.id, '.implode(',', $elasticSearchIds).')');
|
|
$query->orderByRaw('FIELD(products.id, '.implode(',', $elasticSearchIds).')');
|
|
|
} else {
|
|
} else {
|
|
|
switch ($sortKey) {
|
|
switch ($sortKey) {
|
|
|
|
|
+ case 'FEATURED':
|
|
|
|
|
+ $query->leftJoin('product_categories', function ($join) use ($sortCategoryId) {
|
|
|
|
|
+ $join->on('products.id', '=', 'product_categories.product_id');
|
|
|
|
|
+
|
|
|
|
|
+ if ($sortCategoryId) {
|
|
|
|
|
+ $join->where('product_categories.category_id', $sortCategoryId);
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ ->orderBy('product_categories.position', 'asc')
|
|
|
|
|
+ ->orderBy('products.id', 'asc')
|
|
|
|
|
+ ->select('products.*');
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case 'BEST_SELLING':
|
|
|
|
|
+ $query->leftJoin('order_items', 'products.id', '=', 'order_items.product_id')
|
|
|
|
|
+ ->groupBy('products.id')
|
|
|
|
|
+ ->orderByRaw('COALESCE(SUM(order_items.qty_ordered), 0) desc')
|
|
|
|
|
+ ->orderBy('products.id', 'asc')
|
|
|
|
|
+ ->select('products.*');
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case 'NEWEST':
|
|
|
|
|
+ $query->orderBy('products.created_at', 'desc')
|
|
|
|
|
+ ->orderBy('products.id', 'desc');
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case 'OLDEST':
|
|
|
|
|
+ $query->orderBy('products.created_at', 'asc')
|
|
|
|
|
+ ->orderBy('products.id', 'asc');
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
case 'TITLE':
|
|
case 'TITLE':
|
|
|
case 'NAME':
|
|
case 'NAME':
|
|
|
$prefix = \DB::getTablePrefix();
|
|
$prefix = \DB::getTablePrefix();
|