diff --git a/packages/table-rate-shipping/resources/lang/en/relationmanagers.php b/packages/table-rate-shipping/resources/lang/en/relationmanagers.php index 0db0284a8..5f63385cb 100644 --- a/packages/table-rate-shipping/resources/lang/en/relationmanagers.php +++ b/packages/table-rate-shipping/resources/lang/en/relationmanagers.php @@ -34,9 +34,12 @@ 'currency_id' => [ 'label' => 'Currency', ], - 'min_quantity' => [ + 'min_spend' => [ 'label' => 'Min. Spend', ], + 'min_weight' => [ + 'label' => 'Min. Weight (KG)', + ], 'price' => [ 'label' => 'Price', ], diff --git a/packages/table-rate-shipping/src/Filament/Resources/ShippingZoneResource/Pages/ManageShippingRates.php b/packages/table-rate-shipping/src/Filament/Resources/ShippingZoneResource/Pages/ManageShippingRates.php index 9d84b2f26..93e9b1a0d 100644 --- a/packages/table-rate-shipping/src/Filament/Resources/ShippingZoneResource/Pages/ManageShippingRates.php +++ b/packages/table-rate-shipping/src/Filament/Resources/ShippingZoneResource/Pages/ManageShippingRates.php @@ -5,6 +5,7 @@ use Awcodes\Shout\Components\Shout; use Filament\Forms; use Filament\Forms\Form; +use Filament\Forms\Get; use Filament\Resources\Pages\ManageRelatedRecords; use Filament\Support\Facades\FilamentIcon; use Filament\Tables; @@ -16,6 +17,8 @@ use Lunar\Models\CustomerGroup; use Lunar\Models\Price; use Lunar\Shipping\Filament\Resources\ShippingZoneResource; +use Lunar\Shipping\Models\Contracts\ShippingMethod as ShippingMethodContract; +use Lunar\Shipping\Models\ShippingMethod; use Lunar\Shipping\Models\ShippingRate; class ManageShippingRates extends ManageRelatedRecords @@ -58,6 +61,7 @@ function () { __('lunarpanel.shipping::relationmanagers.shipping_rates.form.shipping_method_id.label') ) ->required() + ->live() ->relationship(name: 'shippingMethod', titleAttribute: 'name') ->columnSpan(2), Forms\Components\TextInput::make('price') @@ -98,28 +102,39 @@ function () { )->default( Currency::getDefault()->id )->required()->preload(), - Forms\Components\TextInput::make('min_quantity') + Forms\Components\TextInput::make('price') ->label( - __('lunarpanel.shipping::relationmanagers.shipping_rates.form.prices.repeater.min_quantity.label') + __('lunarpanel.shipping::relationmanagers.shipping_rates.form.prices.repeater.price.label') ) ->numeric() ->required(), - Forms\Components\TextInput::make('price') + Forms\Components\TextInput::make('min_quantity') ->label( - __('lunarpanel.shipping::relationmanagers.shipping_rates.form.prices.repeater.price.label') + function (Get $get) { + if (static::getShippingChargeBy($get('../../shipping_method_id')) == 'weight') { + return __('lunarpanel.shipping::relationmanagers.shipping_rates.form.prices.repeater.min_weight.label'); + } + + return __('lunarpanel.shipping::relationmanagers.shipping_rates.form.prices.repeater.min_spend.label'); + } ) ->numeric() ->required(), ])->afterStateHydrated( static function (Forms\Components\Repeater $component, ?Model $record = null): void { if ($record) { + $chargeBy = static::getShippingChargeBy($record->shippingMethod); + $currencies = Currency::all(); + $component->state( - $record->priceBreaks->map(function ($price) { + $record->priceBreaks->map(function ($price) use ($chargeBy, $currencies) { + $currency = $currencies->first(fn ($currency) => $currency->id == $price->currency_id); + return [ 'customer_group_id' => $price->customer_group_id, 'price' => $price->price->decimal, 'currency_id' => $price->currency_id, - 'min_quantity' => $price->min_quantity / 100, + 'min_quantity' => $chargeBy == 'cart_total' ? $price->min_quantity / $currency->factor : $price->min_quantity / 100, ]; })->toArray() ); @@ -166,6 +181,19 @@ public function table(Table $table): Table ]); } + private static function getShippingChargeBy(ShippingMethodContract|int|null $method): string + { + if (blank($method)) { + return 'cart_total'; + } + + if (! $method instanceof ShippingMethodContract) { + $method = ShippingMethod::find($method); + } + + return ($method?->data['charge_by'] ?? null) ?? 'cart_total'; + } + protected static function saveShippingRate(?ShippingRate $shippingRate = null, array $data = []): void { $currency = Currency::getDefault(); @@ -182,11 +210,17 @@ protected static function saveShippingRate(?ShippingRate $shippingRate = null, a $shippingRate->priceBreaks()->delete(); $currencies = Currency::all(); + $chargeBy = static::getShippingChargeBy($shippingRate->shippingMethod); + $tiers = collect($data['prices'] ?? [])->map( - function ($price) use ($currencies) { + function ($price) use ($chargeBy, $currencies) { $currency = $currencies->first(fn ($currency) => $currency->id == $price['currency_id']); - $price['min_quantity'] = (int) ($price['min_quantity'] * $currency->factor); + if ($chargeBy == 'cart_total') { + $price['min_quantity'] = (int) ($price['min_quantity'] * $currency->factor); + } else { + $price['min_quantity'] = (int) ($price['min_quantity'] * 100); + } $price['price'] = (int) ($price['price'] * $currency->factor);