Deploy Ladill Woo Manager / deploy (push) Successful in 39s
Auto-import catalog and orders after store activation, add manual order sync, and allow WooCommerce webhooks without CSRF tokens. Co-authored-by: Cursor <cursoragent@cursor.com>
87 lines
2.8 KiB
PHP
87 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Woo;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\WooOrder;
|
|
use App\Models\WooStore;
|
|
use App\Services\Woo\FulfillmentSyncService;
|
|
use App\Services\Woo\OrderSyncService;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\View\View;
|
|
|
|
class OrderController extends Controller
|
|
{
|
|
public function __construct(
|
|
private FulfillmentSyncService $sync,
|
|
private OrderSyncService $orderSync,
|
|
) {}
|
|
|
|
public function index(Request $request): View
|
|
{
|
|
$user = $request->user();
|
|
$search = trim((string) $request->query('q', ''));
|
|
$storeFilter = $request->query('store');
|
|
|
|
$storeIds = WooStore::query()->where('user_id', $user->id)->pluck('id');
|
|
|
|
$orders = WooOrder::query()
|
|
->whereIn('woo_store_id', $storeIds)
|
|
->when($storeFilter, fn ($q) => $q->where('woo_store_id', $storeFilter))
|
|
->when($search !== '', function ($query) use ($search) {
|
|
$like = '%'.$search.'%';
|
|
$query->where(function ($inner) use ($like) {
|
|
$inner->where('customer_name', 'like', $like)
|
|
->orWhere('customer_email', 'like', $like)
|
|
->orWhere('order_number', 'like', $like);
|
|
});
|
|
})
|
|
->with('store')
|
|
->latest('created_at')
|
|
->paginate(20)
|
|
->withQueryString();
|
|
|
|
$stores = WooStore::query()->where('user_id', $user->id)->orderBy('site_name')->get();
|
|
|
|
return view('woo.orders.index', compact('orders', 'stores', 'search', 'storeFilter'));
|
|
}
|
|
|
|
public function updateStatus(Request $request, WooOrder $order): RedirectResponse
|
|
{
|
|
$order->load('store');
|
|
abort_if($order->store?->user_id !== $request->user()->id, 403);
|
|
|
|
$validated = $request->validate([
|
|
'fulfillment_status' => ['required', Rule::in(array_keys(WooOrder::FULFILLMENT_STATUSES))],
|
|
]);
|
|
|
|
$order->update(['fulfillment_status' => $validated['fulfillment_status']]);
|
|
$this->sync->push($order->fresh());
|
|
|
|
return back()->with('success', 'Fulfillment status updated.');
|
|
}
|
|
|
|
public function sync(Request $request): RedirectResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'store' => ['required', 'integer'],
|
|
]);
|
|
|
|
$store = WooStore::query()
|
|
->where('user_id', $request->user()->id)
|
|
->whereKey((int) $validated['store'])
|
|
->where('status', WooStore::STATUS_ACTIVE)
|
|
->firstOrFail();
|
|
|
|
$count = $this->orderSync->syncOrders($store);
|
|
|
|
return back()->with('success', sprintf(
|
|
'Synced %d orders from %s.',
|
|
$count,
|
|
$store->site_name ?? $store->site_url,
|
|
));
|
|
}
|
|
}
|