Unexpected Logged-In User Detection in merx.completePayment:after Hook

Hi everyone,

I’m developing a website using the Merx plugin and need help with a specific issue related to user detection during order processing.

I’m using the ww.merx.completePayment:after hook to send a confirmation email and optionally create a user account for customers who are not already registered:

'ww.merx.completePayment:after' => function (OrderPage $orderPage) {
    // Send confirmation email
    sendConfirmationMail($orderPage);
};

In my sendConfirmationMail() function, I check whether a user is currently logged in before deciding whether to create a new user account:

function sendConfirmationMail($orderPage) {
    $site = site();
    $toEmail = $site->toemail()->value();
    $fromEmail = $site->fromemail()->value();

    $loginuser = kirby()->user();

    if ($loginuser && $loginuser->isLoggedIn()) {
        echo "Logged in as: " . $loginuser->id();
        dump($loginuser);
        die();
    } else {
        echo "No user logged in";
        die();
    }
}

The issue:
Even when I place an order as a guest (i.e. not logged in), kirby()->user() still returns a logged-in user, and isLoggedIn() is true. Upon checking, I noticed that the user returned is kirby@getkirby.com.

I would like to know:

  • Does Merx automatically authenticate or impersonate a dummy user (like kirby@getkirby.com) during guest checkout?
  • If so, is there a way to distinguish between real logged-in users and internally created/system users?

My goal is to store the user ID with each order, so that orders can be listed per user on their frontend dashboard.

Best regards,
Shiv

issue has been resolved with $kirby->impersonate(null)

Merx uses $kirby->impersonate(‘kirby’) before calling the ww.merx.completePayment:after hook. (see merx/src/Merx.php at a5dff42a155eb7a2908d3126908c6ba6ebbc0bba · wagnerwagner/merx · GitHub)