diff --git a/packages/admin/src/Filament/Resources/ProductResource/Widgets/ProductOptionsWidget.php b/packages/admin/src/Filament/Resources/ProductResource/Widgets/ProductOptionsWidget.php index a082be186d..2a50f3e01f 100644 --- a/packages/admin/src/Filament/Resources/ProductResource/Widgets/ProductOptionsWidget.php +++ b/packages/admin/src/Filament/Resources/ProductResource/Widgets/ProductOptionsWidget.php @@ -245,7 +245,7 @@ public function mapVariantPermutations($fillMissing = true): void )] )->toArray(); - $variants = $this->record->variants->load('values.option')->map(function ($variant) { + $variants = $this->record->variants->load(['basePrices.currency', 'basePrices.priceable', 'values.option'])->map(function ($variant) { return [ 'id' => $variant->id, 'sku' => $variant->sku, diff --git a/packages/core/database/factories/OrderFactory.php b/packages/core/database/factories/OrderFactory.php index 7ec861a3d4..456201bc73 100644 --- a/packages/core/database/factories/OrderFactory.php +++ b/packages/core/database/factories/OrderFactory.php @@ -13,7 +13,7 @@ class OrderFactory extends BaseFactory public function definition(): array { $total = $this->faker->numberBetween(200, 25000); - $taxTotal = ($total - 100) * .2; + $taxTotal = intval(($total - 100) * .2); return [ 'channel_id' => Channel::factory(), diff --git a/packages/core/src/Base/ModelManifest.php b/packages/core/src/Base/ModelManifest.php index 68999a9bcc..7ab6677be8 100644 --- a/packages/core/src/Base/ModelManifest.php +++ b/packages/core/src/Base/ModelManifest.php @@ -143,7 +143,7 @@ public function guessModelClass(string $modelContract): string return 'Lunar\\Models\\'.$shortName; } - public function isLunarModel(BaseModel $model): bool + public function isLunarModel(string|BaseModel $model): bool { $class = (new \ReflectionClass($model)); diff --git a/packages/core/src/Base/Traits/HasModelExtending.php b/packages/core/src/Base/Traits/HasModelExtending.php index 7306c70705..c0a0412d94 100644 --- a/packages/core/src/Base/Traits/HasModelExtending.php +++ b/packages/core/src/Base/Traits/HasModelExtending.php @@ -3,6 +3,7 @@ namespace Lunar\Base\Traits; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\Arr; use Lunar\Facades\ModelManifest; @@ -46,6 +47,23 @@ public static function modelClass(): string return ModelManifest::get($contractClass) ?? static::class; } + public function getMorphClass(): string + { + $morphMap = Relation::morphMap(); + + if ($customModelMorphMap = array_search(static::modelClass(), $morphMap, true)) { + return $customModelMorphMap; + } + + $parentClass = get_parent_class(static::class); + + if (ModelManifest::isLunarModel($parentClass) && $lunarModelMorphMap = array_search($parentClass, $morphMap, true)) { + return $lunarModelMorphMap; + } + + return parent::getMorphClass(); + } + public static function isLunarInstance(): bool { return static::class == static::modelClass(); diff --git a/packages/core/src/Facades/ModelManifest.php b/packages/core/src/Facades/ModelManifest.php index 700071d594..877de04cea 100644 --- a/packages/core/src/Facades/ModelManifest.php +++ b/packages/core/src/Facades/ModelManifest.php @@ -16,7 +16,7 @@ * @method static string|null get(string $interfaceClass) * @method static string guessContractClass(string $modelClass) * @method static string guessModelClass(string $modelContract) - * @method static bool isLunarModel(BaseModel $model) + * @method static bool isLunarModel(string|BaseModel $model) * @method static string getTable(BaseModel $model) * @method static void morphMap() * @method static string getMorphMapKey(string $className) diff --git a/tests/core/Unit/Base/Traits/HasModelExtendingTest.php b/tests/core/Unit/Base/Traits/HasModelExtendingTest.php index 40e8e95e7c..413a276eb9 100644 --- a/tests/core/Unit/Base/Traits/HasModelExtendingTest.php +++ b/tests/core/Unit/Base/Traits/HasModelExtendingTest.php @@ -45,3 +45,15 @@ function () { expect($newStaticMethod)->toBeInstanceOf(Collection::class); expect($newStaticMethod)->toHaveCount(3); }); + +test('morph map is correct when models are extended', function () { + \Lunar\Facades\ModelManifest::replace( + \Lunar\Models\Contracts\Product::class, + \Lunar\Tests\Core\Stubs\Models\CustomProduct::class + ); + + expect((new \Lunar\Tests\Core\Stubs\Models\CustomProduct)->getMorphClass()) + ->toBe('product') + ->and((new Product)->getMorphClass()) + ->toBe('product'); +});