Kaynağa Gözat

查询产品详情

bianjunhui 5 gün önce
ebeveyn
işleme
c5d18301a9

+ 0 - 15
packages/Webkul/BagistoApi/src/Dto/ProductDetail/ProductAttributeDto.php

@@ -1,15 +0,0 @@
-<?php
-
-namespace Webkul\BagistoApi\Dto\ProductDetail;
-
-use ApiPlatform\Metadata\ApiResource;
-
-#[ApiResource(operations: [], graphQlOperations: [])]
-class ProductAttributeDto
-{
-    public function __construct(
-        public ?string $code = null,
-        public ?string $label = null,
-        public mixed $value = null,
-    ) {}
-}

+ 12 - 5
packages/Webkul/BagistoApi/src/Dto/ProductDetail/ProductDetailDto.php

@@ -26,12 +26,19 @@ class ProductDetailDto
     public ?string $short_description = null;
 
     /**
-     * Customer Features HTML (textarea attribute with code "customer_features").
-     * Returns null when the attribute is missing or empty.
+     * Video attribute value (text).
      */
-    /** @var ProductAttributeDto[] All custom attribute values for this product. */
-    #[ApiProperty(readableLink: true)]
-    public array $attributes = [];
+    public ?string $video = null;
+
+    /**
+     * Alone attribute value (boolean).
+     */
+    public ?bool $alone = null;
+
+    /**
+     * Customer Features HTML (textarea attribute with code "feature").
+     */
+    public ?string $feature = null;
 
     public ?float $price = null;
 

+ 50 - 52
packages/Webkul/BagistoApi/src/Models/Product.php

@@ -445,6 +445,9 @@ class Product extends BaseProduct
         'color'                => ['id' => 23],
         'size'                 => ['id' => 24],
         'brand'                => ['id' => 25],
+        'video'                => ['id' => 49],
+        'alone'                => ['id' => 36],
+        'feature'              => ['id' => 37],
     ];
 
     #[ApiProperty(identifier: true, writable: false)]
@@ -504,71 +507,66 @@ class Product extends BaseProduct
     }
 
     /**
-     * Get all custom attribute values for this product.
-     *
-     * Aggregates every `attribute_values` row into a flat list of
-     * `{ code, label, value }` entries (resolved for the current
-     * locale/channel). Exposed to GraphQL/REST as `attributes` so any
-     * attribute added later in the Attributes table is surfaced
-     * automatically without code changes.
-     *
-     * @return \Webkul\BagistoApi\Dto\ProductDetail\ProductAttributeDto[]
+     * Get the "video" attribute value (text).
      */
-    public function getAttributesAttribute(): array
+    public function getVideoAttribute(): ?string
     {
-        if (! $this->relationLoaded('attribute_values')) {
-            $this->load('attribute_values.attribute');
-        } elseif (! $this->attribute_values->first()?->relationLoaded('attribute')) {
-            $this->load('attribute_values.attribute');
-        }
-
-        $currentLocale = $this->locale ?? app()->getLocale();
-        $currentChannel = $this->channel ?? (core()->getCurrentChannel()->code ?? 'default');
-
-        $localeVariants = array_values(array_unique(array_filter([
-            $currentLocale,
-            core()->getCurrentChannel()->default_locale?->code,
-            null,
-        ])));
+        $value = $this->getSystemAttributeValue('video');
 
-        $channelVariants = [$currentChannel, null];
+        return $value === '' ? null : (string) $value;
+    }
 
-        $seen = [];
-        $result = [];
+    #[ApiProperty(writable: true, readable: true)]
+    public function getVideo(): ?string
+    {
+        return $this->getVideoAttribute();
+    }
 
-        foreach ($this->attribute_values as $av) {
-            $code = $av->attribute?->code;
+    public function setVideo(?string $value): void
+    {
+        $this->setSystemAttributeValue('video', $value);
+    }
 
-            if (! $code) {
-                continue;
-            }
+    /**
+     * Get the "alone" attribute value (boolean).
+     */
+    public function getAloneAttribute(): ?bool
+    {
+        $value = $this->getSystemAttributeValue('alone');
 
-            // Only keep the best (most specific) match per attribute code.
-            if (isset($seen[$code])) {
-                continue;
-            }
+        return $value === '' || $value === null ? null : (bool) $value;
+    }
 
-            $locale = $av->locale;
-            $channel = $av->channel;
+    #[ApiProperty(writable: true, readable: true)]
+    public function getAlone(): ?bool
+    {
+        return $this->getAloneAttribute();
+    }
 
-            $localeRank = array_search($locale, $localeVariants, true);
-            $channelRank = array_search($channel, $channelVariants, true);
+    public function setAlone(?bool $value): void
+    {
+        $this->setSystemAttributeValue('alone', $value);
+    }
 
-            // Ignore rows that don't match the current locale/channel scope.
-            if ($localeRank === false || $channelRank === false) {
-                continue;
-            }
+    /**
+     * Get the "feature" attribute value (textarea / HTML).
+     */
+    public function getFeatureAttribute(): ?string
+    {
+        $value = $this->getSystemAttributeValue('feature');
 
-            $seen[$code] = true;
+        return $value === '' ? null : (string) $value;
+    }
 
-            $result[] = new \Webkul\BagistoApi\Dto\ProductDetail\ProductAttributeDto(
-                code: $code,
-                label: $av->attribute?->admin_name ?? $av->attribute?->name ?? $code,
-                value: $av->value,
-            );
-        }
+    #[ApiProperty(writable: true, readable: true)]
+    public function getFeature(): ?string
+    {
+        return $this->getFeatureAttribute();
+    }
 
-        return $result;
+    public function setFeature(?string $value): void
+    {
+        $this->setSystemAttributeValue('feature', $value);
     }
 
     /**

+ 3 - 1
packages/Webkul/BagistoApi/src/State/ProductDetailProvider.php

@@ -77,7 +77,9 @@ class ProductDetailProvider implements ProviderInterface
         $dto->status = (bool) $product->status;
         $dto->description = $product->description;
         $dto->short_description = $product->short_description;
-        $dto->attributes = $product->attributes;
+        $dto->video = $product->video;
+        $dto->alone = $product->alone;
+        $dto->feature = $product->feature;
         $dto->price = $product->price !== null ? (float) $product->price : null;
         $dto->special_price = $product->special_price !== null ? (float) $product->special_price : null;
         $dto->new = (bool) $product->new;