Jelajahi Sumber

增加字段

bianjunhui 6 hari lalu
induk
melakukan
29dbb84d3b

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

@@ -53,6 +53,21 @@ class CategoryProducts
      */
     public int $total_count = 0;
 
+    /**
+     * Category logo URL.
+     */
+    public ?string $logo = null;
+
+    /**
+     * Category banner URL.
+     */
+    public ?string $banner = null;
+
+    /**
+     * Category description.
+     */
+    public ?string $description = null;
+
     /**
      * Product list for the current page.
      *

+ 13 - 5
packages/Webkul/BagistoApi/src/Resolver/CategoryProductsResolver.php

@@ -4,10 +4,10 @@ namespace Webkul\BagistoApi\Resolver;
 
 use ApiPlatform\GraphQl\Resolver\QueryItemResolverInterface;
 use ApiPlatform\State\Pagination\PaginatorInterface;
-use Illuminate\Support\Facades\DB;
 use Webkul\BagistoApi\Dto\ProductSearch\ProductSearchEdgeDto;
 use Webkul\BagistoApi\Models\CategoryProducts;
 use Webkul\BagistoApi\State\ProductGraphQLProvider;
+use Webkul\Category\Models\Category;
 
 class CategoryProductsResolver implements QueryItemResolverInterface
 {
@@ -30,14 +30,17 @@ class CategoryProductsResolver implements QueryItemResolverInterface
             return new CategoryProducts;
         }
 
-        $categoryId = DB::table('category_translations')
-            ->where('slug', $categorySlug)
-            ->value('category_id');
+        $locale = $args['locale'] ?? null;
+        $category = Category::query()
+            ->whereHas('translations', fn ($q) => $q->where('slug', $categorySlug))
+            ->first();
 
-        if (! $categoryId) {
+        if (! $category) {
             throw new \RuntimeException("Category not found for slug: {$categorySlug}");
         }
 
+        $categoryId = $category->id;
+
         $filters = $this->parseFilter($args['filter'] ?? null);
         $filters['category_id'] = (int) $categoryId;
 
@@ -62,6 +65,11 @@ class CategoryProductsResolver implements QueryItemResolverInterface
         $result = new CategoryProducts;
         $result->total_count = (int) $paginator->getTotalItems();
 
+        // Attach category logo / banner / description.
+        $result->logo = $category->logo_url;
+        $result->banner = $category->banner_url;
+        $result->description = $category->translate($locale)?->description;
+
         $offset = $this->resolveOffset($args, $result->total_count);
         $limit = max(1, (int) ($args['first'] ?? $args['last'] ?? 30));