Skip to content

Conversation

@fceprika
Copy link

Description

The collect() method in Magento\Sales\Model\Order\Creditmemo\Total\Tax has a partial guard at line 137 that checks $orderShippingAmount > 0 before dividing at line 139, but line 140 divides by $baseOrderShippingAmount without any zero-check.

When an order has free shipping (base_shipping_amount = 0), this causes a DivisionByZeroError at line 140:

$basePart = $creditmemo->getBaseShippingAmount() / $baseOrderShippingAmount;

This is inconsistent with line 109 in the same method, which correctly guards $baseOrderShippingAmount before dividing:

if ($baseOrderShippingAmount && $creditmemo->getBaseShippingAmount() !== null) {
    $taxFactor = $creditmemo->getBaseShippingAmount() / $baseOrderShippingAmount;

Steps to reproduce

  1. Place an order with free shipping (shipping_amount = 0, base_shipping_amount = 0)
  2. Create an invoice for the order
  3. Trigger credit memo creation via CreditmemoFactory::createByOrder()
  4. DivisionByZeroError is thrown at Tax.php:140

In production, this is triggered by the Stripe webhook retry cron (stripe_webhook_events_retry) when it attempts to cancel/refund an order with free shipping that has an existing invoice.

Expected result

Credit memo is created without error. When shipping amounts are zero, the shipping tax proportion block is skipped entirely (no shipping tax to apportion).

Actual result

DivisionByZeroError: Division by zero in vendor/magento/module-sales/Model/Order/Creditmemo/Total/Tax.php:140

This error repeats on every cron cycle, blocking webhook retry processing.

Fix

Add $baseOrderShippingAmount > 0 to the existing guard clause at line 137, so both divisors ($orderShippingAmount at line 139 and $baseOrderShippingAmount at line 140) are protected.

Related

The invoice path (line 109) already has the correct guard. This fix makes the non-invoice path consistent.

@m2-assistant
Copy link

m2-assistant bot commented Jan 28, 2026

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

@Den4ik
Copy link
Contributor

Den4ik commented Jan 28, 2026

@fceprika Thanks for PR.
Please sign CLA.

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.

2 participants