Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Lunar\Base\Migration;
use Lunar\Models\Channel;
use Lunar\Shipping\Models\ShippingMethod;

return new class extends Migration
{
public function up(): void
{
Schema::create($this->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');
}
};
3 changes: 3 additions & 0 deletions resources/lang/en/relationmanagers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,7 +17,6 @@ class ManageShippingMethodAvailability extends BaseManageRelatedRecords

public function getTitle(): string
{

return __('lunarpanel.shipping::shippingmethod.pages.availability.label');
}

Expand All @@ -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'),
]),
Expand Down
19 changes: 19 additions & 0 deletions src/Models/ShippingMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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();
}
}
23 changes: 18 additions & 5 deletions src/ShippingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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');
});
Expand All @@ -73,7 +86,7 @@ public function boot(ShippingModifiers $shippingModifiers)
});

ModelManifest::addDirectory(
__DIR__.'/Models'
__DIR__ . '/Models'
);

Relation::morphMap([
Expand Down