Deploy Ladill Woo Manager / deploy (push) Successful in 51s
Free: 1 store and 20 products. Pro/Business mirrors Invoice pricing (GHS 49/149) with wallet renewals, Paystack prepay, session-based store switcher, and scoped UI. Co-authored-by: Cursor <cursoragent@cursor.com>
59 lines
1.3 KiB
PHP
59 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
use App\Models\User;
|
|
use App\Models\WooStore;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class CurrentWooStore
|
|
{
|
|
public const SESSION_KEY = 'woo.current_store_id';
|
|
|
|
public function activeStores(User $user): Collection
|
|
{
|
|
return WooStore::query()
|
|
->where('user_id', $user->id)
|
|
->where('status', WooStore::STATUS_ACTIVE)
|
|
->orderBy('site_name')
|
|
->orderBy('site_url')
|
|
->get();
|
|
}
|
|
|
|
public function resolve(?User $user): ?WooStore
|
|
{
|
|
if (! $user) {
|
|
return null;
|
|
}
|
|
|
|
$stores = $this->activeStores($user);
|
|
if ($stores->isEmpty()) {
|
|
return null;
|
|
}
|
|
|
|
$selectedId = (int) session(self::SESSION_KEY, 0);
|
|
$store = $stores->firstWhere('id', $selectedId);
|
|
if ($store) {
|
|
return $store;
|
|
}
|
|
|
|
$first = $stores->first();
|
|
session([self::SESSION_KEY => $first->id]);
|
|
|
|
return $first;
|
|
}
|
|
|
|
public function switch(User $user, int $storeId): WooStore
|
|
{
|
|
$store = WooStore::query()
|
|
->where('user_id', $user->id)
|
|
->whereKey($storeId)
|
|
->where('status', WooStore::STATUS_ACTIVE)
|
|
->firstOrFail();
|
|
|
|
session([self::SESSION_KEY => $store->id]);
|
|
|
|
return $store;
|
|
}
|
|
}
|