Skip to content

Adjust CP request handling for console requests - #4343

Merged
lukeholder merged 2 commits into
5.xfrom
tommysvr-patch-1
Aug 7, 2026
Merged

Adjust CP request handling for console requests#4343
lukeholder merged 2 commits into
5.xfrom
tommysvr-patch-1

Conversation

@tommysvr

Copy link
Copy Markdown
Contributor

Description

Calling Order::getPdfUrl() from the console throws 'Calling unknown method: craft\console\Request::setIsCpRequest()' because setIsCpRequest() is only defined on craft\web\Request, not craft\console\Request.

This guards it so setIsCpRequest is skipped outside a web request.

Test command to run:

public function actionTestCpRequest(): int {
  $order = Order::find()->isCompleted(true)->orderBy('id desc')->one();

  if (!$order) {
    $this->stdout("No completed order found to test with.\n");
    return ExitCode::UNSPECIFIED_ERROR;
  }

  $url = $order->getPdfUrl();
  $this->stdout("URL: $url\n");
  return ExitCode::OK;
}

Related issues

Via Support

tommysvr and others added 2 commits July 28, 2026 11:40
Prevent setting CP request flag for console requests.

Test command to run:

```
public function actionTestCpRequest(): int {
  $order = Order::find()->isCompleted(true)->orderBy('id desc')->one();

  if (!$order) {
    $this->stdout("No completed order found to test with.\n");
    return ExitCode::UNSPECIFIED_ERROR;
  }

  $url = $order->getPdfUrl();
  $this->stdout("URL: $url\n");
  return ExitCode::OK;
}
```
Only skip setIsCpRequest() when it was already true, instead of
special-casing console requests — this naturally covers console
requests too, since craft\console\Request::getIsCpRequest() is
hardcoded false. Also apply the same fix to getLoadCartUrl(), which
had the identical bug and is reachable from order email templates via
order.loadCartUrl.
@lukeholder
lukeholder marked this pull request as ready for review August 7, 2026 03:09
@lukeholder
lukeholder requested a review from a team as a code owner August 7, 2026 03:09
@lukeholder
lukeholder merged commit 20415de into 5.x Aug 7, 2026
13 checks passed
@lukeholder
lukeholder deleted the tommysvr-patch-1 branch August 7, 2026 03:10
@lukeholder

Copy link
Copy Markdown
Member

Thanks for this. I pushed a follow-up commit with two changes:

Simplified the guard instead of checking getIsConsoleRequest() around each call, it now checks if ($isCpRequest) using the value already fetched.

craft\console\Request::getIsCpRequest() is hardcoded to always return false, so this naturally skips the setIsCpRequest() calls on console requests too.

It also skips the no-op setIsCpRequest(false) on ordinary site requests, and wraps the URL generation in try/finally so the flag is restored even if UrlHelper::actionUrl() throws just in case.

Applied the identical fix to Carts::getLoadCartUrl(), which had the same unguarded pattern and is exposed via order.loadCartUrl in templates so it would hit the same fatal error from a queue-sent order email that references that URL.

@lukeholder

Copy link
Copy Markdown
Member

To get the fix early, change your craftcms/commerce requirement in composer.json to:

"require": {
  "craftcms/commerce": "5.x-dev#20415deaa8352fa1038df42960ea1ccd6e63e09a as 5.7.1",
  "...": "..."
}

Then run composer update.

We will update this ticket once the release is out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants