Files
ladill-woo-manager/app/Support/CurrentWooStore.php
T
isaaccladandCursor 09359ca86a
Deploy Ladill Woo Manager / deploy (push) Successful in 39s
Add per-store managers for Woo Business accounts.
Agencies can invite store-scoped managers via Identity, with access limited to one WooCommerce store while owners retain full multi-store control.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-08 07:51:08 +00:00

78 lines
2.0 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';
/** @param list<int>|null $allowedStoreIds */
public function activeStores(User $account, ?array $allowedStoreIds = null): Collection
{
$query = WooStore::query()
->where('user_id', $account->id)
->where('status', WooStore::STATUS_ACTIVE)
->orderBy('site_name')
->orderBy('site_url');
if ($allowedStoreIds !== null) {
$query->whereIn('id', $allowedStoreIds);
}
return $query->get();
}
/** @param list<int>|null $allowedStoreIds */
public function resolve(?User $account, ?array $allowedStoreIds = null): ?WooStore
{
if (! $account) {
return null;
}
$stores = $this->activeStores($account, $allowedStoreIds);
if ($stores->isEmpty()) {
return null;
}
if ($allowedStoreIds !== null && count($allowedStoreIds) === 1) {
session([self::SESSION_KEY => $stores->first()->id]);
return $stores->first();
}
$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;
}
/** @param list<int>|null $allowedStoreIds */
public function switch(User $account, int $storeId, ?array $allowedStoreIds = null): WooStore
{
$query = WooStore::query()
->where('user_id', $account->id)
->whereKey($storeId)
->where('status', WooStore::STATUS_ACTIVE);
if ($allowedStoreIds !== null) {
$query->whereIn('id', $allowedStoreIds);
}
$store = $query->firstOrFail();
session([self::SESSION_KEY => $store->id]);
return $store;
}
}