Adjust CP request handling for console requests - #4343
Conversation
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.
|
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.
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. |
|
To get the fix early, change your "require": {
"craftcms/commerce": "5.x-dev#20415deaa8352fa1038df42960ea1ccd6e63e09a as 5.7.1",
"...": "..."
}Then run We will update this ticket once the release is out. |
Description
Calling Order::getPdfUrl() from the console throws
'Calling unknown method: craft\console\Request::setIsCpRequest()'becausesetIsCpRequest()is only defined oncraft\web\Request, notcraft\console\Request.This guards it so
setIsCpRequestis skipped outside a web request.Test command to run:
Related issues
Via Support