Explorar o código

添加赠品字段

llp hai 1 semana
pai
achega
be135633ca
Modificáronse 1 ficheiros con 22 adicións e 4 borrados
  1. 22 4
      packages/Webkul/BagistoApi/src/Dto/CartItemData.php

+ 22 - 4
packages/Webkul/BagistoApi/src/Dto/CartItemData.php

@@ -2,6 +2,7 @@
 
 namespace Webkul\BagistoApi\Dto;
 
+use ApiPlatform\Metadata\ApiProperty;
 use ApiPlatform\Metadata\ApiResource;
 use ApiPlatform\Metadata\GraphQl\Query;
 use Symfony\Component\Serializer\Annotation\Groups;
@@ -104,6 +105,16 @@ class CartItemData
     #[Groups(['query', 'mutation'])]
     public ?bool $canChangeQty = null;
 
+    /**
+     * 是否赠品行(Longyi\FreeGift 在 additional 里打标,见 Config\Gift::CART_ITEM_FLAG)。
+     *
+     * 与 REST 的 Webkul\Shop\Http\Resources\CartItemResource::$is_free_gift 口径一致,
+     * 前端据此展示赠品徽章。
+     */
+    #[Groups(['query', 'mutation'])]
+    #[ApiProperty(description: 'Whether this cart item is an auto-added free gift')]
+    public ?bool $isFreeGift = null;
+
     /**
      * Create CartItemData from CartItem model
      */
@@ -119,6 +130,15 @@ class CartItemData
         $data->quantity = (int) $item->quantity;
         $data->type = $item->type;
 
+        // Additional payload(历史数据可能是 JSON 字符串)
+        $additional = $item->additional ?
+            (is_string($item->additional) ? json_decode($item->additional, true) : $item->additional) : [];
+
+        // 赠品行:数量由赠品规则决定,不允许用户改
+        $isFreeGift = ! empty($additional['free_gift_rule_id'] ?? null);
+
+        $data->isFreeGift = $isFreeGift;
+
         // Base prices
         $data->price = (float) core()->convertPrice($item->base_price ?? 0);
         $data->basePrice = (float) ($item->base_price ?? 0);
@@ -148,9 +168,6 @@ class CartItemData
         $data->baseTaxAmount = (float) ($item->base_tax_amount ?? 0);
 
         // Product info - extract formatted attributes (bundle options, configurable options, etc.)
-        $additional = $item->additional ?
-            (is_string($item->additional) ? json_decode($item->additional, true) : $item->additional) : [];
-
         $attributes = ! empty($additional['attributes'])
             ? array_values($additional['attributes'])
             : null;
@@ -170,7 +187,8 @@ class CartItemData
                 $data->baseImage = null;
             }
             $data->productUrlKey = $item->product->url_key;
-            $data->canChangeQty = $item->product->getTypeInstance()->showQuantityBox();
+            $data->canChangeQty = $item->product->getTypeInstance()->showQuantityBox()
+                && ! $isFreeGift;
         }
 
         return $data;