Skip to content
Merged
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,22 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Lunar\Base\Migration;

return new class extends Migration
{
public function up(): void
{
Schema::table($this->prefix.'product_variants', function (Blueprint $table) {
$table->string('weight_unit')->default('kg')->nullable()->change();
});
}

public function down(): void
{
Schema::table($this->prefix.'product_variants', function (Blueprint $table) {
$table->string('weight_unit')->default('mm')->nullable()->change();
});
}
};
39 changes: 39 additions & 0 deletions packages/core/database/state/UpdateWeightUnitToKg.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Lunar\Database\State;

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

class UpdateWeightUnitToKg
{
public function prepare()
{
//
}

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

DB::table($this->table())
->whereNull('weight_unit')
->orWhere('weight_unit', 'mm')
->update(['weight_unit' => 'kg']);
}

protected function canRun(): bool
{
return Schema::hasTable($this->table())
&& Schema::hasColumn($this->table(), 'weight_unit');
}

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

return $prefix.'product_variants';
}
}
10 changes: 9 additions & 1 deletion packages/core/src/LunarServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
use Lunar\Database\State\EnsureMediaCollectionsAreRenamed;
use Lunar\Database\State\MigrateCartOrderRelationship;
use Lunar\Database\State\PopulateProductOptionLabelWithName;
use Lunar\Database\State\UpdateWeightUnitToKg;
use Lunar\Facades\Telemetry;
use Lunar\Listeners\CartSessionAuthListener;
use Lunar\Managers\CartSessionManager;
Expand Down Expand Up @@ -302,6 +303,7 @@ protected function registerStateListeners()
MigrateCartOrderRelationship::class,
ConvertTaxbreakdown::class,
ConvertBackOrderPurchasability::class,
UpdateWeightUnitToKg::class,
];

foreach ($states as $state) {
Expand Down Expand Up @@ -392,9 +394,15 @@ protected function registerBlueprintMacros(): void
Blueprint::macro('dimensions', function () {
/** @var Blueprint $this */
$columns = ['length', 'width', 'height', 'weight', 'volume'];
$unitDefaults = [
'weight' => 'kg',
];

foreach ($columns as $column) {
$this->decimal("{$column}_value", 10, 4)->default(0)->nullable()->index();
$this->string("{$column}_unit")->default('mm')->nullable();
$this->string("{$column}_unit")
->default($unitDefaults[$column] ?? 'mm')
->nullable();
}
});

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Models/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public function scopeActive(Builder $query): Builder
->whereDoesntHave('orders')
->orWhereHas('orders', function ($sub) {
$sub->whereNull('placed_at');
});
});
});
}

Expand Down
44 changes: 22 additions & 22 deletions tests/core/Unit/Models/CartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1168,29 +1168,29 @@
setAuthUserConfig();

$currency = Currency::factory()->create();
$channel = Channel::factory()->create();
$channel = Channel::factory()->create();

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

$otherUsersCart = Cart::factory()->create([
'user_id' => $userB->id,
'user_id' => $userB->id,
'currency_id' => $currency->id,
'channel_id' => $channel->id,
'channel_id' => $channel->id,
]);

$expectedCart = Cart::factory()->create([
'user_id' => $userA->id,
$expectedCart = Cart::factory()->create([
'user_id' => $userA->id,
'currency_id' => $currency->id,
'channel_id' => $channel->id,
'merged_id' => null,
'channel_id' => $channel->id,
'merged_id' => null,
]);

$mergedCart = Cart::factory()->create([
'user_id' => $userA->id,
'user_id' => $userA->id,
'currency_id' => $currency->id,
'channel_id' => $channel->id,
'merged_id' => $expectedCart->id,
'channel_id' => $channel->id,
'merged_id' => $expectedCart->id,
]);

$cartId = $userA->carts()
Expand All @@ -1208,28 +1208,28 @@
setAuthUserConfig();

$currency = Currency::factory()->create();
$channel = Channel::factory()->create();
$user = StubUser::factory()->create();
$channel = Channel::factory()->create();
$user = StubUser::factory()->create();

$older = Cart::factory()->create([
'user_id' => $user->id,
'merged_id' => null,
'user_id' => $user->id,
'merged_id' => null,
'currency_id' => $currency->id,
'channel_id' => $channel->id,
'channel_id' => $channel->id,
]);

$expectedCart = Cart::factory()->create([
'user_id' => $user->id,
'merged_id' => null,
'user_id' => $user->id,
'merged_id' => null,
'currency_id' => $currency->id,
'channel_id' => $channel->id,
'channel_id' => $channel->id,
]);

$mergedCart = Cart::factory()->create([
'user_id' => $user->id,
'merged_id' => $expectedCart->id,
'user_id' => $user->id,
'merged_id' => $expectedCart->id,
'currency_id' => $currency->id,
'channel_id' => $channel->id,
'channel_id' => $channel->id,
]);

$this->actingAs($user);
Expand All @@ -1242,4 +1242,4 @@
->and($foundCart->id)->not->toBe($older->id)
->and($foundCart->id)->not->toBe($mergedCart->id)
->and($foundCart->merged_id)->toBeNull();
});
});