chengwl пре 2 недеља
родитељ
комит
30eb098c4d
1 измењених фајлова са 54 додато и 7 уклоњено
  1. 54 7
      packages/Webkul/BagistoApi/src/Models/CustomerOrderItem.php

+ 54 - 7
packages/Webkul/BagistoApi/src/Models/CustomerOrderItem.php

@@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
 use Illuminate\Database\Eloquent\Relations\HasMany;
 use Illuminate\Database\Eloquent\Relations\HasOne;
 use Illuminate\Database\Eloquent\Relations\MorphTo;
+use Webkul\Product\Facades\ProductImage;
 
 /**
  * Customer Order Item — nested API resource (no standalone endpoints).
@@ -20,7 +21,7 @@ use Illuminate\Database\Eloquent\Relations\MorphTo;
     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', 'product', 'children']],
+    normalizationContext: ['attributes' => ['id','qty_ordered', 'qty_shipped', 'qty_invoiced', 'qty_canceled', 'qty_refunded', 'sku', 'name', 'price', 'base_price', 'total', 'base_total', 'type', 'product', 'children','baseImage']],
 )]
 class CustomerOrderItem extends Model
 {
@@ -28,7 +29,7 @@ class CustomerOrderItem extends Model
     protected $table = 'order_items';
 
     /** @var array */
-    protected $appends = ['qtyOrdered', 'qtyShipped', 'qtyInvoiced', 'qtyCanceled', 'qtyRefunded'];
+    protected $appends = ['qtyOrdered', 'qtyShipped', 'qtyInvoiced', 'qtyCanceled', 'qtyRefunded', 'baseImage'];
 
     /** @var array */
     protected $casts = [
@@ -161,12 +162,23 @@ class CustomerOrderItem extends Model
     }
 
     /**
-     * Polymorphic product relationship.
+     * Product main image (same structure as cart item base_image).
      *
-     * 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.
+     * @return array<string, string>|null
+     */
+    #[ApiProperty(writable: false, description: 'Product base image URLs (small/medium/large/original)')]
+    public function getBaseImage(): ?array
+    {
+        return $this->resolveBaseImage();
+    }
+
+    public function getBaseImageAttribute(): ?array
+    {
+        return $this->resolveBaseImage();
+    }
+
+    /**
+     * Linked product record (morph).
      */
     public function product(): MorphTo
     {
@@ -195,6 +207,40 @@ class CustomerOrderItem extends Model
     public function children(): HasMany
     {
         return $this->hasMany(static::class, 'parent_id');
+ 
+    }
+
+    /**
+     * Resolve the product whose image should represent this line item.
+     */
+    private function resolveImageProduct()
+    {
+        $this->loadMissing(['product.images', 'children.product.images']);
+
+        if ($this->type === 'configurable') {
+            $child = $this->children->first();
+
+            if ($child?->product && $child->product->images->isNotEmpty()) {
+                return $child->product;
+            }
+        }
+
+        return $this->product;
+    }
+
+    private function resolveBaseImage(): ?array
+    {
+        $product = $this->resolveImageProduct();
+
+        if (! $product) {
+            return null;
+        }
+
+        try {
+            return ProductImage::getProductBaseImage($product);
+        } catch (\Throwable) {
+            return null;
+        }
     }
 
     /**
@@ -210,6 +256,7 @@ class CustomerOrderItem extends Model
         $array['qty_invoiced'] = $this->qty_invoiced;
         $array['qty_canceled'] = $this->qty_canceled;
         $array['qty_refunded'] = $this->qty_refunded;
+        $array['baseImage'] = $this->baseImage;
 
         return $array;
     }