Sfoglia il codice sorgente

Merge branch 'dev' of http://gogs.hnwmzp.cn/chengwenliang/nshop into dev

chengwl 1 giorno fa
parent
commit
ad9b8fb12c

+ 1 - 0
packages/Longyi/DynamicMenu/README.md

@@ -21,3 +21,4 @@ php artisan cache:clear      # 清除应用缓存
 php artisan config:clear     # 清除配置缓存
 php artisan view:clear       # 清除视图缓存
 php artisan route:clear      # 清除路由缓存
+php artisan bagisto-api-platform:clear-cache

+ 8 - 0
packages/Webkul/BagistoApi/src/Models/CategoryProducts.php

@@ -24,6 +24,14 @@ use Webkul\BagistoApi\Resolver\CategoryProductsResolver;
                     'type'        => 'String',
                     'description' => 'JSON filter object containing attribute filters. Example: {"color":{"match":"2"},"size":{"match":"M"}}',
                 ],
+                'sortKey' => [
+                    'type'        => 'String',
+                    'description' => 'Sort key. One of: FEATURED (default), BEST_SELLING, NEWEST, OLDEST, PRICE.',
+                ],
+                'reverse' => [
+                    'type'        => 'Boolean',
+                    'description' => 'Reverse the sort direction.',
+                ],
                 'first'   => ['type' => 'Int'],
                 'last'    => ['type' => 'Int'],
                 'after'   => ['type' => 'String'],

+ 2 - 2
packages/Webkul/BagistoApi/src/Resolver/CategoryProductsResolver.php

@@ -43,9 +43,9 @@ class CategoryProductsResolver implements QueryItemResolverInterface
 
         $productArgs = $args;
         $productArgs['filter'] = json_encode($filters);
-        // Default sort: cheapest first (sortKey PRICE, ascending).
+        // Default sort: featured (by product_categories.position, ascending).
         if (! isset($productArgs['sortKey'])) {
-            $productArgs['sortKey'] = 'PRICE';
+            $productArgs['sortKey'] = 'FEATURED';
             $productArgs['reverse'] = false;
         }
 

+ 44 - 0
packages/Webkul/BagistoApi/src/State/ProductGraphQLProvider.php

@@ -92,10 +92,54 @@ class ProductGraphQLProvider implements ProviderInterface
         $locale = $args['locale'] ?? request()->attributes->get('bagisto_locale');
         $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'])) {
             $query->orderByRaw('FIELD(products.id, '.implode(',', $elasticSearchIds).')');
         } else {
             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 'NAME':
                     $prefix = \DB::getTablePrefix();