Skip to content

Commit 7a1e65e

Browse files
authored
Fix default weight unit for product variants (lunarphp#2384)
1 parent 0c140bd commit 7a1e65e

5 files changed

Lines changed: 93 additions & 24 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Support\Facades\Schema;
5+
use Lunar\Base\Migration;
6+
7+
return new class extends Migration
8+
{
9+
public function up(): void
10+
{
11+
Schema::table($this->prefix.'product_variants', function (Blueprint $table) {
12+
$table->string('weight_unit')->default('kg')->nullable()->change();
13+
});
14+
}
15+
16+
public function down(): void
17+
{
18+
Schema::table($this->prefix.'product_variants', function (Blueprint $table) {
19+
$table->string('weight_unit')->default('mm')->nullable()->change();
20+
});
21+
}
22+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Lunar\Database\State;
4+
5+
use Illuminate\Support\Facades\DB;
6+
use Illuminate\Support\Facades\Schema;
7+
8+
class UpdateWeightUnitToKg
9+
{
10+
public function prepare()
11+
{
12+
//
13+
}
14+
15+
public function run(): void
16+
{
17+
if (! $this->canRun()) {
18+
return;
19+
}
20+
21+
DB::table($this->table())
22+
->whereNull('weight_unit')
23+
->orWhere('weight_unit', 'mm')
24+
->update(['weight_unit' => 'kg']);
25+
}
26+
27+
protected function canRun(): bool
28+
{
29+
return Schema::hasTable($this->table())
30+
&& Schema::hasColumn($this->table(), 'weight_unit');
31+
}
32+
33+
protected function table(): string
34+
{
35+
$prefix = config('lunar.database.table_prefix');
36+
37+
return $prefix.'product_variants';
38+
}
39+
}

packages/core/src/LunarServiceProvider.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
use Lunar\Database\State\EnsureMediaCollectionsAreRenamed;
5757
use Lunar\Database\State\MigrateCartOrderRelationship;
5858
use Lunar\Database\State\PopulateProductOptionLabelWithName;
59+
use Lunar\Database\State\UpdateWeightUnitToKg;
5960
use Lunar\Facades\Telemetry;
6061
use Lunar\Listeners\CartSessionAuthListener;
6162
use Lunar\Managers\CartSessionManager;
@@ -302,6 +303,7 @@ protected function registerStateListeners()
302303
MigrateCartOrderRelationship::class,
303304
ConvertTaxbreakdown::class,
304305
ConvertBackOrderPurchasability::class,
306+
UpdateWeightUnitToKg::class,
305307
];
306308

307309
foreach ($states as $state) {
@@ -392,9 +394,15 @@ protected function registerBlueprintMacros(): void
392394
Blueprint::macro('dimensions', function () {
393395
/** @var Blueprint $this */
394396
$columns = ['length', 'width', 'height', 'weight', 'volume'];
397+
$unitDefaults = [
398+
'weight' => 'kg',
399+
];
400+
395401
foreach ($columns as $column) {
396402
$this->decimal("{$column}_value", 10, 4)->default(0)->nullable()->index();
397-
$this->string("{$column}_unit")->default('mm')->nullable();
403+
$this->string("{$column}_unit")
404+
->default($unitDefaults[$column] ?? 'mm')
405+
->nullable();
398406
}
399407
});
400408

packages/core/src/Models/Cart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public function scopeActive(Builder $query): Builder
261261
->whereDoesntHave('orders')
262262
->orWhereHas('orders', function ($sub) {
263263
$sub->whereNull('placed_at');
264-
});
264+
});
265265
});
266266
}
267267

tests/core/Unit/Models/CartTest.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,29 +1168,29 @@
11681168
setAuthUserConfig();
11691169

11701170
$currency = Currency::factory()->create();
1171-
$channel = Channel::factory()->create();
1171+
$channel = Channel::factory()->create();
11721172

11731173
$userA = StubUser::factory()->create();
11741174
$userB = StubUser::factory()->create();
11751175

11761176
$otherUsersCart = Cart::factory()->create([
1177-
'user_id' => $userB->id,
1177+
'user_id' => $userB->id,
11781178
'currency_id' => $currency->id,
1179-
'channel_id' => $channel->id,
1179+
'channel_id' => $channel->id,
11801180
]);
11811181

1182-
$expectedCart = Cart::factory()->create([
1183-
'user_id' => $userA->id,
1182+
$expectedCart = Cart::factory()->create([
1183+
'user_id' => $userA->id,
11841184
'currency_id' => $currency->id,
1185-
'channel_id' => $channel->id,
1186-
'merged_id' => null,
1185+
'channel_id' => $channel->id,
1186+
'merged_id' => null,
11871187
]);
11881188

11891189
$mergedCart = Cart::factory()->create([
1190-
'user_id' => $userA->id,
1190+
'user_id' => $userA->id,
11911191
'currency_id' => $currency->id,
1192-
'channel_id' => $channel->id,
1193-
'merged_id' => $expectedCart->id,
1192+
'channel_id' => $channel->id,
1193+
'merged_id' => $expectedCart->id,
11941194
]);
11951195

11961196
$cartId = $userA->carts()
@@ -1208,28 +1208,28 @@
12081208
setAuthUserConfig();
12091209

12101210
$currency = Currency::factory()->create();
1211-
$channel = Channel::factory()->create();
1212-
$user = StubUser::factory()->create();
1211+
$channel = Channel::factory()->create();
1212+
$user = StubUser::factory()->create();
12131213

12141214
$older = Cart::factory()->create([
1215-
'user_id' => $user->id,
1216-
'merged_id' => null,
1215+
'user_id' => $user->id,
1216+
'merged_id' => null,
12171217
'currency_id' => $currency->id,
1218-
'channel_id' => $channel->id,
1218+
'channel_id' => $channel->id,
12191219
]);
12201220

12211221
$expectedCart = Cart::factory()->create([
1222-
'user_id' => $user->id,
1223-
'merged_id' => null,
1222+
'user_id' => $user->id,
1223+
'merged_id' => null,
12241224
'currency_id' => $currency->id,
1225-
'channel_id' => $channel->id,
1225+
'channel_id' => $channel->id,
12261226
]);
12271227

12281228
$mergedCart = Cart::factory()->create([
1229-
'user_id' => $user->id,
1230-
'merged_id' => $expectedCart->id,
1229+
'user_id' => $user->id,
1230+
'merged_id' => $expectedCart->id,
12311231
'currency_id' => $currency->id,
1232-
'channel_id' => $channel->id,
1232+
'channel_id' => $channel->id,
12331233
]);
12341234

12351235
$this->actingAs($user);
@@ -1242,4 +1242,4 @@
12421242
->and($foundCart->id)->not->toBe($older->id)
12431243
->and($foundCart->id)->not->toBe($mergedCart->id)
12441244
->and($foundCart->merged_id)->toBeNull();
1245-
});
1245+
});

0 commit comments

Comments
 (0)