Navigating Magento 2 Credit Memo Complexities: Unraveling Shipping Discount Refund Errors

In the intricate world of e-commerce, accurate financial transactions are paramount. For Magento 2 store owners, processing refunds is a routine yet critical operation that demands precision. However, a significant bug identified in Magento 2.4.x (Issue #41434) has brought to light a concerning flaw in how credit memos handle shipping discount refunds, particularly for orders involving both shipping tax and a cart price rule applying a shipping discount.

The Hidden Flaw in Magento 2 Credit Memos

The core of the problem lies in situations where a credit memo is created with "Refund Shipping" set to 0, or when no shipping amount is explicitly provided for refund. On orders that have shipping taxed and also benefit from a cart price rule that discounts shipping, Magento's calculation for the shipping discount refund goes awry. This results in either an over-refund or an under-refund, directly impacting financial accuracy and potentially requiring manual adjustments.

Consider an example:

  • Expected Result: When refunding one item and none of the shipping, the grand total should reflect only the item's value, less its proportional discount and tax.
  • Actual Result: The system incorrectly calculates the discount, leading to a grand total that is either higher (over-refund) or lower (under-refund) than what the customer actually paid for the refunded items. This discrepancy can be as specific as a $2.40 over-refund per item, or a $1.60 under-refund for a full order via API.

This issue has been reported on Magento 2.4-develop and reproduced on versions up to 2.4.7-p8, with the affected code remaining consistent across several newer patch versions including 2.4.7-p10, 2.4.8-p5, and 2.4.9. Its severity has been classified as S1, indicating it affects critical functionality and necessitates a workaround, such as manual "Adjustment Fees" to balance the credit memo.

Unpacking the Technical Root Cause

The detailed investigation reveals the bug's origin within the Magento\Sales\Model\Order\Creditmemo\Total\Discount class, specifically during the execution of its sort order 150. This class is responsible for refunding the shipping discount in proportion to the shipping amount being refunded by the credit memo. The critical miscalculation occurs when the credit memo's base_shipping_amount is 0 or not set.

At this stage, the method attempts to derive the shipping amount using base_shipping_incl_tax - base_shipping_tax_amount. However, the shipping and tax totals haven't been fully processed yet. Consequently:

  • When "Refund Shipping = 0": initData() sets base_shipping_incl_tax to 0, but base_shipping_tax_amount still holds the order's original shipping tax. This leads to a negative shipping amount calculation (0 - original shipping tax), resulting in a negative shipping discount.
  • When "No shipping amount" is given: Both fields retain the order's original values. The shipping_incl_tax reflects the shipping plus tax before discount, while shipping_tax_amount is the tax after discount. This mismatch leads to an incorrect shipping calculation (e.g., 110 - 6 = 104 instead of 100). Furthermore, if partial shipping was refunded previously, the system still uses the whole order's shipping, not the remaining amount.

This timing and data inconsistency is the heart of the problem. It's also noted that a related issue, #22343 from 2019, attempted to address a similar "0 vs. not set" scenario but was closed without a complete resolution, leaving part of this problem unaddressed.

What This Means for Your Magento Store

For merchants running Magento 2.4.x with complex pricing rules involving shipping discounts and taxes, this bug can lead to significant headaches. Inaccurate refunds not only impact your bottom line but can also erode customer trust. Developers need to be aware of this specific calculation flaw when building or debugging custom refund logic or integrating with external systems via the Magento API (e.g., POST /V1/order/{orderId}/refund).

The good news is that a resolution is anticipated. The issue's release note indicates that "Credit memos now refund the correct share of the shipping discount when Refund Shipping is 0, or when no shipping amount is given, for orders that have both a shipping discount and shipping tax." This suggests a fix will be rolled out in future Magento updates, providing much-needed relief for affected stores.

Until then, understanding this bug is crucial for accurate financial reporting and customer satisfaction. Always thoroughly test your refund processes, especially after applying discounts and taxes, to ensure your Magento 2 store operates with maximum precision.

Start with the tools

Explore migration tools

See options, compare methods, and pick the path that fits your store.

Explore migration tools