Browse Source

修改订单详情中缺少产品模型的问题

llp 1 day ago
parent
commit
8eb19fbe27
1 changed files with 44 additions and 3 deletions
  1. 44 3
      packages/Webkul/BagistoApi/src/Models/CustomerOrderItem.php

+ 44 - 3
packages/Webkul/BagistoApi/src/Models/CustomerOrderItem.php

@@ -5,6 +5,10 @@ namespace Webkul\BagistoApi\Models;
 use ApiPlatform\Metadata\ApiProperty;
 use ApiPlatform\Metadata\ApiResource;
 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
+use Illuminate\Database\Eloquent\Relations\HasMany;
+use Illuminate\Database\Eloquent\Relations\HasOne;
+use Illuminate\Database\Eloquent\Relations\MorphTo;
 
 /**
  * Customer Order Item — nested API resource (no standalone endpoints).
@@ -16,7 +20,7 @@ use Illuminate\Database\Eloquent\Model;
     shortName: 'CustomerOrderItem',
     operations: [],
     graphQlOperations: [],
-    normalizationContext: ['attributes' => ['id','qty_ordered', 'qty_shipped', 'qty_invoiced', 'qty_canceled', 'qty_refunded', 'sku', 'name', 'price', 'base_price', 'total', 'base_total', 'type']],
+    normalizationContext: ['attributes' => ['id','qty_ordered', 'qty_shipped', 'qty_invoiced', 'qty_canceled', 'qty_refunded', 'sku', 'name', 'price', 'base_price', 'total', 'base_total', 'type', 'product', 'children']],
 )]
 class CustomerOrderItem extends Model
 {
@@ -156,20 +160,57 @@ class CustomerOrderItem extends Model
         return $this->base_total;
     }
 
+    /**
+     * Polymorphic product relationship.
+     *
+     * Mirrors OrderItem::product() — uses the order_items table's
+     * product_id and product_type columns. The BagistoApi Product model
+     * overrides getMorphClass() to return the base class name so that
+     * MorphTo resolution works seamlessly.
+     */
+    public function product(): MorphTo
+    {
+        return $this->morphTo();
+    }
+
+    /**
+     * Get the child item record associated with the order item.
+     */
+    public function child(): HasOne
+    {
+        return $this->hasOne(static::class, 'parent_id');
+    }
+
+    /**
+     * Get the parent item record associated with the order item.
+     */
+    public function parent(): BelongsTo
+    {
+        return $this->belongsTo(static::class, 'parent_id');
+    }
+
+    /**
+     * Get the children items (configurable product variants).
+     */
+    public function children(): HasMany
+    {
+        return $this->hasMany(static::class, 'parent_id');
+    }
+
     /**
      * Override toArray to ensure qty fields are always included in serialization
      */
     public function toArray(): array
     {
         $array = parent::toArray();
-        
+
         // Ensure qty fields are always present
         $array['qty_ordered'] = $this->qty_ordered;
         $array['qty_shipped'] = $this->qty_shipped;
         $array['qty_invoiced'] = $this->qty_invoiced;
         $array['qty_canceled'] = $this->qty_canceled;
         $array['qty_refunded'] = $this->qty_refunded;
-        
+
         return $array;
     }
 }