Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Lunar\Shipping\Database\State;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class RescaleWeightBasedShippingMinQuantity
{
public function prepare(): void
{
//
}

public function run(): void
{
if (! $this->canRun()) {
return;
}

$prefix = config('lunar.database.table_prefix');

$weightMethodIds = DB::table("{$prefix}shipping_methods")
->whereJsonContains('data->charge_by', 'weight')
->pluck('id');

if ($weightMethodIds->isEmpty()) {
return;
}

$shippingRateIds = DB::table("{$prefix}shipping_rates")
->whereIn('shipping_method_id', $weightMethodIds)
->pluck('id');

if ($shippingRateIds->isEmpty()) {
return;
}

DB::table("{$prefix}prices")
->where('priceable_type', 'shipping_rate')
->whereIn('priceable_id', $shippingRateIds)
->where('min_quantity', '>', 1)
->orderBy('id')
->chunk(100, function ($prices) use ($prefix) {
foreach ($prices as $price) {
DB::table("{$prefix}prices")
->where('id', $price->id)
->update([
'min_quantity' => (int) ($price->min_quantity / 100),
]);
}
});
}

protected function canRun(): bool
{
$prefix = config('lunar.database.table_prefix');

return Schema::hasTable("{$prefix}shipping_methods")
&& Schema::hasTable("{$prefix}shipping_rates")
&& Schema::hasTable("{$prefix}prices");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
],
'min_weight' => [
'label' => 'Min. Weight',
'helper_text' => 'Enter in kg',
],
'price' => [
'label' => 'Price',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
],
'min_weight' => [
'label' => 'Min. súly',
'helper_text' => 'Kg-ban adja meg',
],
'price' => [
'label' => 'Ár',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
],
'min_weight' => [
'label' => 'Greutate min.',
'helper_text' => 'Introduceți în kg',
],
'price' => [
'label' => 'Preț',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Lunar\DataTypes\ShippingOption;
use Lunar\Exceptions\MissingCurrencyPriceException;
use Lunar\Facades\Converter;
use Lunar\Facades\Pricing;
use Lunar\Models\Product;
use Lunar\Shipping\DataTransferObjects\ShippingOptionRequest;
Expand Down Expand Up @@ -72,7 +73,17 @@ public function resolve(ShippingOptionRequest $shippingOptionRequest): ?Shipping

if ($chargeBy == 'weight') {
$tier = $cart->lines->sum(function ($line) {
return $line->purchasable->weight_value * $line->quantity;
$weightUnit = array_key_exists($line->purchasable->weight_unit, Converter::getMeasurements()['weight'] ?? [])
? $line->purchasable->weight_unit
: 'kg';

$unitWeightKg = Converter::from("weight.{$weightUnit}")
->to('weight.kg')
->value($line->purchasable->weight_value)
->convert()
->getValue();

return $unitWeightKg * $line->quantity;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ function (Get $get) {
return __('lunarpanel.shipping::relationmanagers.shipping_rates.form.prices.repeater.min_spend.label');
}
)
->helperText(
function (Get $get) {
if (static::getShippingChargeBy($get('../../shipping_method_id')) == 'weight') {
return __('lunarpanel.shipping::relationmanagers.shipping_rates.form.prices.repeater.min_weight.helper_text');
}

return null;
}
)
->suffix(
function (Get $get) {
if (static::getShippingChargeBy($get('../../shipping_method_id')) == 'weight') {
return 'kg';
}

return null;
}
)
->numeric()
->required(),
])->afterStateHydrated(
Expand All @@ -142,7 +160,7 @@ static function (Forms\Components\Repeater $component, ?Model $record = null): v
'customer_group_id' => $price->customer_group_id,
'price' => $price->price->decimal,
'currency_id' => $price->currency_id,
'min_quantity' => $chargeBy == 'cart_total' ? $price->min_quantity / $currency->factor : $price->min_quantity / 100,
'min_quantity' => $chargeBy == 'cart_total' ? $price->min_quantity / $currency->factor : $price->min_quantity,
];
})->toArray()
);
Expand Down Expand Up @@ -259,7 +277,7 @@ function ($price) use ($chargeBy, $currencies) {
if ($chargeBy == 'cart_total') {
$price['min_quantity'] = (int) ($price['min_quantity'] * $currency->factor);
} else {
$price['min_quantity'] = (int) ($price['min_quantity'] * 100);
$price['min_quantity'] = (int) $price['min_quantity'];
Comment thread
Huncsuga marked this conversation as resolved.
}

$price['price'] = (int) ($price['price'] * $currency->factor);
Expand Down
2 changes: 2 additions & 0 deletions packages/table-rate-shipping/src/ShippingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Lunar\Models\Order;
use Lunar\Models\Product;
use Lunar\Shipping\Database\State\MigrateCutoffToSchedule;
use Lunar\Shipping\Database\State\RescaleWeightBasedShippingMinQuantity;
use Lunar\Shipping\DiscountTypes\ShippingDiscount;
use Lunar\Shipping\Interfaces\ShippingMethodManagerInterface;
use Lunar\Shipping\Managers\ShippingManager;
Expand Down Expand Up @@ -100,6 +101,7 @@ public function boot(ShippingModifiers $shippingModifiers)
protected function registerStateListeners(): void
{
$states = [
RescaleWeightBasedShippingMinQuantity::class,
MigrateCutoffToSchedule::class,
];

Expand Down
175 changes: 175 additions & 0 deletions tests/shipping/Unit/Drivers/ShippingMethods/ShipByTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Config;
use Lunar\DataTypes\ShippingOption;
use Lunar\Models\Cart;
use Lunar\Models\Currency;
use Lunar\Models\Price;
use Lunar\Models\ProductVariant;
use Lunar\Models\TaxClass;
use Lunar\Shipping\DataTransferObjects\ShippingOptionRequest;
use Lunar\Shipping\Drivers\ShippingMethods\ShipBy;
Expand Down Expand Up @@ -406,3 +409,175 @@
expect($shippingOption)->toBeInstanceOf(ShippingOption::class);
expect($shippingOption->price->value)->toEqual(800);
});

test('pricing manager resolves correct tier for weight-based shipping using raw kg min_quantity', function () {
$currency = Currency::factory()->create(['default' => true]);
TaxClass::factory()->create(['default' => true]);

$shippingZone = ShippingZone::factory()->create(['type' => 'countries']);

$shippingMethod = ShippingMethod::factory()->create([
'driver' => 'ship-by',
'data' => ['charge_by' => 'weight'],
]);

$shippingRate = ShippingRate::factory()->create([
'shipping_method_id' => $shippingMethod->id,
'shipping_zone_id' => $shippingZone->id,
]);

$shippingRate->prices()->createMany([
['price' => 1000, 'min_quantity' => 1, 'currency_id' => $currency->id],
['price' => 600, 'min_quantity' => 5, 'currency_id' => $currency->id],
['price' => 200, 'min_quantity' => 10, 'currency_id' => $currency->id],
]);

$makeWeightCart = function (float $weightKg) use ($currency): Cart {
$variant = ProductVariant::factory()->create([
'weight_value' => $weightKg,
'weight_unit' => 'kg',
]);
$variant->stock = 100;

Price::factory()->create([
'price' => 500,
'min_quantity' => 1,
'currency_id' => $currency->id,
'priceable_type' => $variant->getMorphClass(),
'priceable_id' => $variant->id,
]);

$cart = Cart::factory()->create(['currency_id' => $currency->id]);
$cart->lines()->create([
'purchasable_type' => $variant->getMorphClass(),
'purchasable_id' => $variant->id,
'quantity' => 1,
]);

return $cart->calculate();
};

$option = (new ShipBy)->resolve(new ShippingOptionRequest(
shippingRate: $shippingRate,
cart: $makeWeightCart(3.0),
));
expect($option)->toBeInstanceOf(ShippingOption::class)
->and($option->price->value)->toEqual(1000);

$option = (new ShipBy)->resolve(new ShippingOptionRequest(
shippingRate: $shippingRate,
cart: $makeWeightCart(5.0),
));
expect($option)->toBeInstanceOf(ShippingOption::class)
->and($option->price->value)->toEqual(600);

$option = (new ShipBy)->resolve(new ShippingOptionRequest(
shippingRate: $shippingRate,
cart: $makeWeightCart(10.0),
));
expect($option)->toBeInstanceOf(ShippingOption::class)
->and($option->price->value)->toEqual(200);
});

test('weight in grams is converted to kg when evaluating weight-based shipping breakpoints', function () {
$currency = Currency::factory()->create(['default' => true]);
TaxClass::factory()->create(['default' => true]);

$shippingZone = ShippingZone::factory()->create(['type' => 'countries']);

$shippingMethod = ShippingMethod::factory()->create([
'driver' => 'ship-by',
'data' => ['charge_by' => 'weight'],
]);

$shippingRate = ShippingRate::factory()->create([
'shipping_method_id' => $shippingMethod->id,
'shipping_zone_id' => $shippingZone->id,
]);

$shippingRate->prices()->createMany([
['price' => 1000, 'min_quantity' => 1, 'currency_id' => $currency->id],
['price' => 500, 'min_quantity' => 5, 'currency_id' => $currency->id],
]);

$variant = ProductVariant::factory()->create([
'weight_value' => 5000.0,
'weight_unit' => 'g',
]);
$variant->stock = 100;

Price::factory()->create([
'price' => 500,
'min_quantity' => 1,
'currency_id' => $currency->id,
'priceable_type' => $variant->getMorphClass(),
'priceable_id' => $variant->id,
]);

$cart = Cart::factory()->create(['currency_id' => $currency->id]);
$cart->lines()->create([
'purchasable_type' => $variant->getMorphClass(),
'purchasable_id' => $variant->id,
'quantity' => 1,
]);
$cart = $cart->calculate();

$option = (new ShipBy)->resolve(new ShippingOptionRequest(
shippingRate: $shippingRate,
cart: $cart,
));

expect($option)->toBeInstanceOf(ShippingOption::class)
->and($option->price->value)->toEqual(500);
});

test('total cart weight across multiple lines with different units is summed in kg for tier matching', function () {
$currency = Currency::factory()->create(['default' => true]);
TaxClass::factory()->create(['default' => true]);

$shippingZone = ShippingZone::factory()->create(['type' => 'countries']);

$shippingMethod = ShippingMethod::factory()->create([
'driver' => 'ship-by',
'data' => ['charge_by' => 'weight'],
]);

$shippingRate = ShippingRate::factory()->create([
'shipping_method_id' => $shippingMethod->id,
'shipping_zone_id' => $shippingZone->id,
]);

$shippingRate->prices()->createMany([
['price' => 1000, 'min_quantity' => 1, 'currency_id' => $currency->id],
['price' => 400, 'min_quantity' => 5, 'currency_id' => $currency->id],
]);

$variantKg = ProductVariant::factory()->create(['weight_value' => 2.0, 'weight_unit' => 'kg']);
$variantKg->stock = 100;
Price::factory()->create([
'price' => 500, 'min_quantity' => 1, 'currency_id' => $currency->id,
'priceable_type' => $variantKg->getMorphClass(), 'priceable_id' => $variantKg->id,
]);

$variantG = ProductVariant::factory()->create(['weight_value' => 1500.0, 'weight_unit' => 'g']);
$variantG->stock = 100;
Price::factory()->create([
'price' => 500, 'min_quantity' => 1, 'currency_id' => $currency->id,
'priceable_type' => $variantG->getMorphClass(), 'priceable_id' => $variantG->id,
]);

$cart = Cart::factory()->create(['currency_id' => $currency->id]);
$cart->lines()->createMany([
['purchasable_type' => $variantKg->getMorphClass(), 'purchasable_id' => $variantKg->id, 'quantity' => 1],
['purchasable_type' => $variantG->getMorphClass(), 'purchasable_id' => $variantG->id, 'quantity' => 2],
]);
$cart = $cart->calculate();

$option = (new ShipBy)->resolve(new ShippingOptionRequest(
shippingRate: $shippingRate,
cart: $cart,
));

expect($option)->toBeInstanceOf(ShippingOption::class)
->and($option->price->value)->toEqual(400);
});