Skip to content

[Issue] fix: adjustCreditMemoItemQuantities causes a fatal error on missing order/invoice #40790

@m2-assistant

Description

@m2-assistant

This issue is automatically created based on existing pull request: #40787: fix: adjustCreditMemoItemQuantities causes a fatal error on missing order/invoice


Preconditions and environment

  • Magento version: 2.4.8-p4
  • Admin user attempting to create a credit memo for an order
  • Occurs when an invalid/missing order_id, creditmemo_id, or invoice_id is passed to the credit memo save action

Steps to reproduce

  1. Navigate to Admin > Sales > Orders and open any order.
  2. Attempt to create a credit memo by navigating directly to the credit memo save URL with an invalid or missing order_id / creditmemo_id parameter (e.g. via a stale or manipulated POST request to admin/sales/order_creditmemo/save).
  3. Submit the form.

Expected result

The controller handles the failed credit memo load gracefully and redirects to the noroute page (or back with an appropriate error), without throwing a fatal error.

Actual result

A TypeError is thrown and logged as a CRITICAL error, crashing the request:

[2026-04-25T08:09:22.547554+00:00] main.CRITICAL: TypeError:
Magento\Sales\Controller\Adminhtml\Order\Creditmemo\Save::adjustCreditMemoItemQuantities():
Argument #1 ($creditMemo) must be of type Magento\Sales\Model\Order\Creditmemo, false given,
called in vendor/magento/module-sales/Controller/Adminhtml/Order/Creditmemo/Save.php on line 88
and defined in vendor/magento/module-sales/Controller/Adminhtml/Order/Creditmemo/Save.php:152

Additional information

Root cause: In Save::execute(), adjustCreditMemoItemQuantities($creditmemo) is called on line 94 before the if ($creditmemo) guard on line 95. When CreditmemoLoader::load() returns false, the typed method receives false instead of a Creditmemo instance, triggering the TypeError.

Fix: Move the adjustCreditMemoItemQuantities($creditmemo) call inside the if ($creditmemo) block so it is only invoked when a valid credit memo object has been loaded.

Release note

Fixed a TypeError crash in the admin credit memo save controller (Magento\Sales\Controller\Adminhtml\Order\Creditmemo\Save) that occurred when the credit memo could not be loaded. The adjustCreditMemoItemQuantities call is now correctly guarded by the existing if ($creditmemo) check.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: OrderComponent: Order gridIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P2A defect with this priority could have functionality issues which are not to expectations.Reported on 2.4.8-p4Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branch

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Ready for Development

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions