[5.x]: Order edit page throws "Can not find a PDF to generate URL" when PDFs exist only on a non-primary store
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 240
- Forks
- 173
- Avg merge
- 5d 11h
- Merged PRs (30d)
- 8
Description
What happened?
This bug report was created with the help of AI, but I have encountered and had to fix it with a small module myself on one of the current commerce sites we have.
Pdfs::getPdfUrl() resolves the PDF against the current store instead of the order's store:
https://github.com/craftcms/commerce/blob/5.x/src/services/Pdfs.php#L472-L479
if ($pdfHandle) {
$pdf = $this->getPdfByHandle($pdfHandle); // no $storeId → current store
} else {
$pdf = $this->getDefaultPdf(); // no $storeId → current store
}
In the control panel the current store is the primary store. OrdersController::_registerJavascript() correctly lists getAllEnabledPdfs($order->storeId), but then calls $order->getPdfUrl(null, $pdf->handle) for each, so any PDF that only exists on a non-primary store is looked up in the wrong store and the order edit page throws:
InvalidArgumentException: Can not find a PDF to generate URL.
DownloadsController::actionPdf() already passes $order->storeId to both lookups, so the fix is to do the same in getPdfUrl():
$pdf = $pdfHandle
? $this->getPdfByHandle($pdfHandle, $order->storeId)
: $this->getDefaultPdf($order->storeId);
Steps to reproduce
- Multi-store install with a primary store and a second store.
- Create a PDF on the second store only (no PDFs on the primary store).
- Open any order belonging to the second store in the control panel.
- The page throws "Can not find a PDF to generate URL."
Also affects order.getPdfUrl() in front-end templates whenever the current site's store differs from the order's store.
Craft CMS version
5.11.x
Craft Commerce version
5.7.4
PHP version
8.4
Operating system and version
No response
Database type and version
No response
Image driver and version
No response
Installed plugins and versions
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/services/Pdfs.php at Pdfs::getPdfUrl(), then compare the store-aware lookups in DownloadsController::actionPdf() and the calls from OrdersController::_registerJavascript(). Done means order edit pages and order.getPdfUrl() resolve PDFs belonging only to the order’s non-primary store instead of throwing the reported exception.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 90/100