Skip to content

fix: adjustCreditMemoItemQuantities causes a fatal error on missing order/invoice#40787

Open
SamJUK wants to merge 1 commit intomagento:2.4-developfrom
SamJUK:fix/creditmemo-adjust-quantities
Open

fix: adjustCreditMemoItemQuantities causes a fatal error on missing order/invoice#40787
SamJUK wants to merge 1 commit intomagento:2.4-developfrom
SamJUK:fix/creditmemo-adjust-quantities

Conversation

@SamJUK
Copy link
Copy Markdown
Contributor

@SamJUK SamJUK commented Apr 27, 2026

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.

Resolved issues:

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

@m2-assistant
Copy link
Copy Markdown

m2-assistant Bot commented Apr 27, 2026

Hi @SamJUK. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.
❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

@ct-prd-pr-scan
Copy link
Copy Markdown

The security team has been informed about this pull request due to the presence of risky security keywords. For security vulnerability reports, please visit Adobe's vulnerability disclosure program on HackerOne or email psirt@adobe.com.

@SamJUK
Copy link
Copy Markdown
Contributor Author

SamJUK commented Apr 27, 2026

@magento run all tests

@SamJUK
Copy link
Copy Markdown
Contributor Author

SamJUK commented Apr 27, 2026

@magento run Unit Tests

@engcom-Hotel
Copy link
Copy Markdown
Contributor

@magento create issue

@ct-prd-pr-scan
Copy link
Copy Markdown

The security team has been informed about this pull request due to the presence of risky security keywords. For security vulnerability reports, please visit Adobe's vulnerability disclosure program on HackerOne or email psirt@adobe.com.

@engcom-Hotel engcom-Hotel added the Priority: P2 A defect with this priority could have functionality issues which are not to expectations. label Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: pending review

Projects

Status: Pending Review

Development

Successfully merging this pull request may close these issues.

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

2 participants