Skip to content

Commit 7ceff18

Browse files
committed
Add UpdateWeightUnitToKg state and update default weight unit to 'kg' in product variants
1 parent 27ffac5 commit 7ceff18

3 files changed

Lines changed: 41 additions & 6 deletions

File tree

packages/core/database/migrations/2025_12_14_215636_update_weight_unit_default_on_product_variants_table.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22

33
use Illuminate\Database\Schema\Blueprint;
4-
use Illuminate\Support\Facades\DB;
54
use Illuminate\Support\Facades\Schema;
65
use Lunar\Base\Migration;
76

@@ -12,11 +11,6 @@ public function up(): void
1211
Schema::table($this->prefix . 'product_variants', function (Blueprint $table) {
1312
$table->string('weight_unit')->default('kg')->nullable()->change();
1413
});
15-
16-
DB::table($this->prefix . 'product_variants')
17-
->whereNull('weight_unit')
18-
->orWhere('weight_unit', 'mm')
19-
->update(['weight_unit' => 'kg']);
2014
}
2115

2216
public function down(): void
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: 2 additions & 0 deletions
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) {

0 commit comments

Comments
 (0)