false, 'ERROR' => 'UserID is required']); } $sql = " SELECT o.ID AS OrderID, o.UUID AS OrderUUID, o.BusinessID, b.Name AS BusinessName, o.OrderTypeID, COALESCE(ot.Name, 'Undecided') AS OrderTypeName, o.ServicePointID, COALESCE(sp.Name, '') AS ServicePointName, (SELECT COUNT(*) FROM OrderLineItems oli WHERE oli.OrderID = o.ID AND oli.ParentOrderLineItemID = 0 AND oli.IsDeleted = 0) AS ItemCount FROM Orders o INNER JOIN Businesses b ON b.ID = o.BusinessID LEFT JOIN tt_OrderTypes ot ON ot.ID = o.OrderTypeID LEFT JOIN ServicePoints sp ON sp.ID = o.ServicePointID WHERE o.UserID = ? AND o.StatusID = 0 "; $params = [$UserID]; if ($BusinessID > 0) { $sql .= " AND o.BusinessID = ?"; $params[] = $BusinessID; } $sql .= " ORDER BY o.AddedOn DESC LIMIT 1"; try { $rows = queryTimed($sql, $params); $cart = $rows[0] ?? null; if (!$cart) { jsonResponse(['OK' => true, 'HAS_CART' => false, 'CART' => null]); } jsonResponse(['OK' => true, 'HAS_CART' => true, 'CART' => [ 'OrderID' => (int) $cart['OrderID'], 'OrderUUID' => $cart['OrderUUID'], 'BusinessID' => (int) $cart['BusinessID'], 'BusinessName' => $cart['BusinessName'], 'OrderTypeID' => (int) $cart['OrderTypeID'], 'OrderTypeName' => $cart['OrderTypeName'], 'ServicePointID' => (int) $cart['ServicePointID'], 'ServicePointName' => $cart['ServicePointName'], 'ItemCount' => (int) $cart['ItemCount'], ]]); } catch (Exception $e) { jsonResponse(['OK' => false, 'ERROR' => $e->getMessage()]); }