chengwl 16 ore fa
parent
commit
f00f223b65

+ 12 - 1
packages/Webkul/BagistoApi/src/Models/Product.php

@@ -493,6 +493,17 @@ class Product extends BaseProduct
 
 
     public $channel;
     public $channel;
 
 
+    /**
+     * Keep the polymorphic type aligned with the base class used by the
+     * Price indexer (Webkul\Product\Models\Product), so morphMany relations
+     * like price_indices() resolve the rows that were written with
+     * priceable_type = Webkul\Product\Models\Product.
+     */
+    public function getMorphClass(): string
+    {
+        return BaseProduct::class;
+    }
+
     protected $with = [
     protected $with = [
         'attribute_family',
         'attribute_family',
         'images',
         'images',
@@ -933,7 +944,7 @@ class Product extends BaseProduct
             $this->setRelation('flexible_variants', $collection);
             $this->setRelation('flexible_variants', $collection);
 
 
             if ($collection->isNotEmpty() && ! $collection->first()->relationLoaded('variant_images')) {
             if ($collection->isNotEmpty() && ! $collection->first()->relationLoaded('variant_images')) {
-                $collection->loadMissing(['variant_images', 'values.option']);
+                $collection->loadMissing(['variant_images', 'values.option','price_indices']);
             }
             }
 
 
             return $collection;
             return $collection;

+ 39 - 2
packages/Webkul/BagistoApi/src/Models/ProductVariant.php

@@ -10,6 +10,8 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany;
 use Longyi\Core\Models\ProductVariant as BaseProductVariant;
 use Longyi\Core\Models\ProductVariant as BaseProductVariant;
 use Spatie\MediaLibrary\MediaCollections\Models\Media;
 use Spatie\MediaLibrary\MediaCollections\Models\Media;
 use Webkul\BagistoApi\Resolver\BaseQueryItemResolver;
 use Webkul\BagistoApi\Resolver\BaseQueryItemResolver;
+use Illuminate\Database\Eloquent\Relations\MorphMany;
+use Webkul\Product\Models\ProductPriceIndexProxy;
 
 
 /**
 /**
  * BagistoApi wrapper around the Longyi1 flexible variant model so that
  * BagistoApi wrapper around the Longyi1 flexible variant model so that
@@ -19,9 +21,10 @@ use Webkul\BagistoApi\Resolver\BaseQueryItemResolver;
 #[ApiResource(
 #[ApiResource(
     routePrefix: '/api/shop',
     routePrefix: '/api/shop',
     shortName: 'ProductVariant',
     shortName: 'ProductVariant',
+    paginationEnabled: false,
     operations: [],
     operations: [],
     graphQlOperations: [
     graphQlOperations: [
-        new Query(resolver: BaseQueryItemResolver::class),
+        new Query(resolver: BaseQueryItemResolver::class, paginationEnabled: false),
     ]
     ]
 )]
 )]
 class ProductVariant extends BaseProductVariant
 class ProductVariant extends BaseProductVariant
@@ -31,8 +34,15 @@ class ProductVariant extends BaseProductVariant
         'effective_price',
         'effective_price',
         'variant_images',
         'variant_images',
         'option_values',
         'option_values',
+        'price_indices',
     ];
     ];
 
 
+   
+    public function getMorphClass(): string
+    {
+        return BaseProductVariant::class;
+    }
+
     #[ApiProperty(identifier: true, writable: false)]
     #[ApiProperty(identifier: true, writable: false)]
     public function getId(): ?int
     public function getId(): ?int
     {
     {
@@ -122,7 +132,7 @@ class ProductVariant extends BaseProductVariant
      */
      */
     public function getEffectivePriceAttribute(): float
     public function getEffectivePriceAttribute(): float
     {
     {
-        return $this->getBasicEffectivePrice();
+        return $this->getEffectivePrice();
     }
     }
 
 
     #[ApiProperty(writable: false, readable: true, required: false)]
     #[ApiProperty(writable: false, readable: true, required: false)]
@@ -226,4 +236,31 @@ class ProductVariant extends BaseProductVariant
     {
     {
         return $this->belongsTo(Product::class, 'product_id');
         return $this->belongsTo(Product::class, 'product_id');
     }
     }
+  
+    public function getPriceIndicesAttribute(): ?string
+    {
+        if (! $this->relationLoaded('price_indices')) {
+            $this->load('price_indices');
+        }
+        $customerGroupId = app(\Webkul\Customer\Repositories\CustomerRepository::class)
+        ->getCurrentGroup()->id;
+        $channelId = core()->getCurrentChannel()->id;
+
+        $indexs = $this->getRelation('price_indices')
+            ->where('customer_group_id', $customerGroupId)
+            ->where('channel_id', $channelId);
+
+
+        $payload = $indexs
+            ->map(fn ($price_index) => [
+            'id'        => $price_index->id,
+            'min_price' => $price_index->min_price,
+            'regular_min_price' => $price_index->regular_min_price,
+        ])
+        ->values()
+        ->all();
+
+        return empty($payload) ? null : json_encode($payload);
+    }
+    
 }
 }