Skip to content

Fix TypeError in Shipping::collectRates() when no carriers are configured - #41169

Open
lbajsarowicz wants to merge 1 commit into
magento:2.4-developfrom
lbajsarowicz:fix/shipping-carriers-null-guard
Open

Fix TypeError in Shipping::collectRates() when no carriers are configured#41169
lbajsarowicz wants to merge 1 commit into
magento:2.4-developfrom
lbajsarowicz:fix/shipping-carriers-null-guard

Conversation

@lbajsarowicz

Copy link
Copy Markdown
Contributor

Description

Magento\Shipping\Model\Shipping::collectRates() iterates the carrier configuration without checking it is an array:

$carriers = $this->_scopeConfig->getValue(
    'carriers',
    \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
    $storeId
);

foreach ($carriers as $carrierCode => $carrierConfig) {

Magento\Config\App\Config\Type\System::getDataByPathParts() returns null, not an empty array, when the requested key is absent from the merged scope data. That happens whenever no carrier module contributes a carriers/* node for the scope — for example with every shipping carrier disabled or uninstalled. On PHP 8 the foreach then throws a TypeError and checkout breaks. ScopeConfigInterface::getValue() is declared @return mixed, so callers cannot assume an array.

Note the asymmetry a few lines below in the same method: the $limitCarrier branch already normalises its value with is_array() before iterating. This applies the same guard to the branch that lacks it.

This is the same bug that was half-fixed in 2021

#30830 reported exactly this failure ("disable all shipping carrier modules" → blank checkout, array_keys(): argument #1 must be of type array, null given). It was fixed by #30822, which guarded the identical getValue('carriers', ...) call sites in Magento\Shipping\Model\Config::getActiveCarriers() and getAllCarriers() — but did not touch Shipping::collectRates(), which has the same unguarded pattern and is still reachable today.

Fixed Issues

No open issue; this is the remaining half of the defect reported in #30830, which was closed when #30822 fixed the sibling call sites.

Manual testing scenarios

  1. Disable every shipping carrier (Stores > Configuration > Sales > Delivery Methods, or uninstall the carrier modules).
  2. Add a product to the cart and open checkout, or call Shipping::collectRates() with a request that has no limitCarrier.
  3. Before: TypeError: foreach() argument must be of type array|object, null given. After: rate collection completes and returns no rates.

Questions or comments

Gates run locally on 2.4-develop (Warden, PHP 8.3):

  • Unit: Shipping/Test/Unit/Model/ShippingTest.php — 3 tests pass, including a new case that mocks the config to return null and asserts collectRates() returns normally instead of throwing.
  • PHPCS Magento2: clean. PHPStan level 1: no errors.
  • Negative check: the new test errors against unpatched Shipping.php and passes with the fix.

No signature changes, so no Semantic Version Checker exposure.

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

getValue('carriers', ...) returns null instead of an empty array when no
carrier module contributes config to the scope (e.g. all shipping carriers
disabled), causing foreach() to throw on PHP 8. Normalize to an array
before iterating, matching the guard already used for $limitCarrier a few
lines below.
@lbajsarowicz

Copy link
Copy Markdown
Contributor Author

@magento run all tests

@m2-assistant

m2-assistant Bot commented Aug 30, 2026

Copy link
Copy Markdown

Hi @lbajsarowicz. 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.

@lbajsarowicz

Copy link
Copy Markdown
Contributor Author

@magento run Unit Tests

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.

1 participant