Chore: Claiming Guest Orders Moved to Core

This commit is contained in:
2026-09-25 13:58:13 +03:00
parent a51e9456d6
commit fa172860db
4 changed files with 5 additions and 63 deletions
@@ -5,7 +5,6 @@
use App\Http\Controllers\Controller;
use App\Mail\EmailChangeCodeMail;
use App\Mail\EmailChangedNoticeMail;
use App\Services\GuestOrderClaimer;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
@@ -14,6 +13,7 @@
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Illuminate\View\View;
use Modules\Core\Customer\Services\GuestOrderClaimer;
/**
* Changing the login email: new email → 6-digit code sent to that address →
@@ -91,7 +91,7 @@ public function resend(string $locale, Request $request): RedirectResponse
return back()->with('status', __('storefront.auth.code_resent'));
}
public function verify(string $locale, Request $request, GuestOrderClaimer $orders): RedirectResponse
public function verify(string $locale, Request $request, GuestOrderClaimer $claimer): RedirectResponse
{
$pending = $request->session()->get(self::SESSION_KEY);
@@ -136,7 +136,7 @@ public function verify(string $locale, Request $request, GuestOrderClaimer $orde
Mail::to($oldEmail)->send(new EmailChangedNoticeMail($pending['email']));
// The code just proved they own the new address too.
$orders->claim($user);
$claimer->claim($user);
$request->session()->forget(self::SESSION_KEY);
-22
View File
@@ -1,22 +0,0 @@
<?php
namespace App\Listeners;
use App\Services\GuestOrderClaimer;
use Modules\Core\Auth\Events\UserAuthenticated;
/**
* Picked up by Laravel's listener discovery (app/Listeners), no manual
* registration. UserAuthenticated only fires after a valid login code.
*/
class ClaimGuestOrdersOnLogin
{
public function __construct(
private readonly GuestOrderClaimer $claimer,
) {}
public function handle(UserAuthenticated $event): void
{
$this->claimer->claim($event->user);
}
}
-37
View File
@@ -1,37 +0,0 @@
<?php
namespace App\Services;
use Illuminate\Contracts\Auth\Authenticatable;
use Lunar\Models\Order;
/**
* Attaches placed guest orders to an account when their billing email matches
* the account's email. Only ever called right after the shopper has proved
* they own that email (a login code, or the code confirming an email change),
* which is what makes matching on email safe.
*
* Orders already belonging to any customer or user are never touched.
*/
class GuestOrderClaimer
{
public function claim(Authenticatable $user): int
{
$customer = $user->latestCustomer();
if (! $customer || ! $user->email) {
return 0;
}
return Order::query()
->whereNotNull('placed_at')
->whereNull('customer_id')
->whereNull('user_id')
->whereHas('billingAddress', fn ($query) => $query
->whereRaw('lower(contact_email) = ?', [strtolower($user->email)]))
->update([
'customer_id' => $customer->id,
'user_id' => $user->id,
]);
}
}
@@ -42,7 +42,8 @@
<p class="bbk-checkout-note">{{ __('checkout.page.confirmation_email_note') }}</p>
{{-- Guests: logging in with the order's email attaches it to an account
(App\Listeners\ClaimGuestOrdersOnLogin), so it shows in their history. --}}
(boboko-core's Modules\Core\Customer\Listeners\ClaimGuestOrdersOnLogin),
so it shows in their history. --}}
@guest
@if ($loginRoute = config('checkout.login_route'))
<p class="bbk-checkout-note">