diff --git a/database/migrations/2025_04_11_100000_create_channel_shipping_method_table.php b/database/migrations/2025_04_11_100000_create_channel_shipping_method_table.php new file mode 100644 index 0000000..b116c22 --- /dev/null +++ b/database/migrations/2025_04_11_100000_create_channel_shipping_method_table.php @@ -0,0 +1,34 @@ +prefix . 'channel_shipping_method', function (Blueprint $table) { + $table->id(); + $table->foreignIdFor(Channel::class)->constrained( + $this->prefix . 'channels' + ); + $table->foreignIdFor(ShippingMethod::class)->constrained( + $this->prefix . 'shipping_methods' + ); + $table->boolean('enabled')->default(true)->index(); + $table->timestamp('starts_at')->nullable()->index(); + $table->timestamp('ends_at')->nullable()->index(); + $table->timestamps(); + + $table->unique(['channel_id', 'shipping_method_id']); + }); + } + + public function down(): void + { + Schema::dropIfExists($this->prefix . 'channel_shipping_method'); + } +}; diff --git a/resources/lang/en/relationmanagers.php b/resources/lang/en/relationmanagers.php index 0db0284..e3f2098 100644 --- a/resources/lang/en/relationmanagers.php +++ b/resources/lang/en/relationmanagers.php @@ -5,6 +5,9 @@ 'customer_groups' => [ 'description' => "Associate customer groups to this shipping method to determine it's availability.", ], + 'channels' => [ + 'description' => "Associate channels to this shipping method to determine it's availability.", + ], ], 'shipping_rates' => [ 'title_plural' => 'Shipping Rates', diff --git a/src/Filament/Resources/ShippingMethodResource/Pages/ListShippingMethod.php b/src/Filament/Resources/ShippingMethodResource/Pages/ListShippingMethod.php index 047617b..e64f1e0 100644 --- a/src/Filament/Resources/ShippingMethodResource/Pages/ListShippingMethod.php +++ b/src/Filament/Resources/ShippingMethodResource/Pages/ListShippingMethod.php @@ -6,6 +6,7 @@ use Filament\Forms\Components\Group; use Lunar\Admin\Support\Pages\BaseListRecords; use Lunar\Models\CustomerGroup; +use Lunar\Models\Channel; use Lunar\Shipping\Filament\Resources\ShippingMethodResource; use Lunar\Shipping\Models\ShippingMethod; @@ -25,9 +26,14 @@ protected function getDefaultHeaderActions(): array ShippingMethodResource::getDescriptionFormComponent(), ])->after(function (ShippingMethod $shippingMethod) { $customerGroups = CustomerGroup::pluck('id')->mapWithKeys( - fn ($id) => [$id => ['visible' => true, 'enabled' => true, 'starts_at' => now()]] + fn($id) => [$id => ['visible' => true, 'enabled' => true, 'starts_at' => now()]] ); $shippingMethod->customerGroups()->sync($customerGroups); + + $channels = Channel::pluck('id')->mapWithKeys( + fn($id) => [$id => ['enabled' => true, 'starts_at' => now()]] + ); + $shippingMethod->channels()->sync($channels); }), ]; } diff --git a/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php b/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php index db2bb40..7ec075c 100644 --- a/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php +++ b/src/Filament/Resources/ShippingMethodResource/Pages/ManageShippingMethodAvailability.php @@ -6,6 +6,7 @@ use Filament\Support\Facades\FilamentIcon; use Lunar\Admin\Filament\Resources\ProductResource\RelationManagers\CustomerGroupRelationManager; use Lunar\Admin\Support\Pages\BaseManageRelatedRecords; +use Lunar\Admin\Support\RelationManagers\ChannelRelationManager; use Lunar\Shipping\Filament\Resources\ShippingMethodResource; class ManageShippingMethodAvailability extends BaseManageRelatedRecords @@ -16,7 +17,6 @@ class ManageShippingMethodAvailability extends BaseManageRelatedRecords public function getTitle(): string { - return __('lunarpanel.shipping::shippingmethod.pages.availability.label'); } @@ -34,6 +34,9 @@ public function getRelationManagers(): array { return [ RelationGroup::make('Availability', [ + ChannelRelationManager::make([ + 'description' => __('lunarpanel.shipping::relationmanagers.shipping_methods.channels.description'), + ]), CustomerGroupRelationManager::make([ 'description' => __('lunarpanel.shipping::relationmanagers.shipping_methods.customer_groups.description'), ]), diff --git a/src/Models/ShippingMethod.php b/src/Models/ShippingMethod.php index 5ef58b5..221036b 100644 --- a/src/Models/ShippingMethod.php +++ b/src/Models/ShippingMethod.php @@ -9,6 +9,7 @@ use Illuminate\Support\Facades\DB; use Lunar\Base\BaseModel; use Lunar\Base\Traits\HasCustomerGroups; +use Lunar\Models\Channel; use Lunar\Models\CustomerGroup; use Lunar\Shipping\Database\Factories\ShippingMethodFactory; use Lunar\Shipping\Facades\Shipping; @@ -36,6 +37,7 @@ protected static function booted() static::deleting(function (self $shippingMethod) { DB::beginTransaction(); $shippingMethod->customerGroups()->detach(); + $shippingMethod->channels()->detach(); $shippingMethod->shippingRates()->delete(); DB::commit(); }); @@ -76,4 +78,21 @@ public function customerGroups(): BelongsToMany 'ends_at', ])->withTimestamps(); } + + /** + * Return the channels relationship. + */ + public function channels(): BelongsToMany + { + $prefix = config('lunar.database.table_prefix'); + + return $this->belongsToMany( + Channel::class, + "{$prefix}channel_shipping_method" + )->withPivot([ + 'enabled', + 'starts_at', + 'ends_at', + ])->withTimestamps(); + } } diff --git a/src/ShippingServiceProvider.php b/src/ShippingServiceProvider.php index a054879..ec55e96 100644 --- a/src/ShippingServiceProvider.php +++ b/src/ShippingServiceProvider.php @@ -23,7 +23,7 @@ class ShippingServiceProvider extends ServiceProvider { public function register() { - $this->mergeConfigFrom(__DIR__.'/../config/shipping-tables.php', 'lunar.shipping-tables'); + $this->mergeConfigFrom(__DIR__ . '/../config/shipping-tables.php', 'lunar.shipping-tables'); } public function boot(ShippingModifiers $shippingModifiers) @@ -32,13 +32,13 @@ public function boot(ShippingModifiers $shippingModifiers) return; } - $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'lunarpanel.shipping'); + $this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'lunarpanel.shipping'); if (! config('lunar.database.disable_migrations', false)) { - $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); + $this->loadMigrationsFrom(__DIR__ . '/../database/migrations'); } - $this->loadViewsFrom(__DIR__.'/../resources/views', 'shipping'); + $this->loadViewsFrom(__DIR__ . '/../resources/views', 'shipping'); $shippingModifiers->add( ShippingModifier::class, @@ -64,6 +64,19 @@ public function boot(ShippingModifiers $shippingModifiers) )->withTimestamps(); }); + \Lunar\Models\Channel::resolveRelationUsing('shippingMethods', function ($channel) { + $prefix = config('lunar.database.table_prefix'); + + return $channel->belongsToMany( + ShippingMethod::class, + "{$prefix}channel_shipping_method" + )->withPivot([ + 'enabled', + 'starts_at', + 'ends_at', + ])->withTimestamps(); + }); + Product::resolveRelationUsing('shippingExclusions', function ($product) { return $product->morphMany(ShippingExclusion::class, 'purchasable'); }); @@ -73,7 +86,7 @@ public function boot(ShippingModifiers $shippingModifiers) }); ModelManifest::addDirectory( - __DIR__.'/Models' + __DIR__ . '/Models' ); Relation::morphMap([