how to create a credit memo in magento programmatically

how to create a credit memo in magento programmatically

August 1, 2026 · Illustrated author avatarBy Stefan Bogdanescu
how to create a credit memo in magento programmatically

Solving Magento Credit Memo Issues Programmatically

Magento provides robust functionality for managing credit memos within your e-commerce store. However, when attempting to create a credit memo programmatically, you might encounter issues where the totals appear as zero.

Let’s delve into why this issue occurs and provide a comprehensive solution using Magento 2 best practices.

Understanding the Problem with Zero Totals

The provided code snippet attempts to create a credit memo by converting an existing order. However, it fails to correctly calculate and update the totals during the process:

$creditmemo->setTotalQty($totalQty);
$creditmemo->collectTotals();

While this looks like the right approach, Magento requires additional steps to properly handle tax calculations for credit memos.

The Correct Approach: Using Magento Native Functions

To create a credit memo with accurate totals, it’s crucial to leverage Magento’s built-in functions designed for this purpose. This ensures that all necessary calculations are performed correctly, including taxes and adjustments.

Here is the corrected code snippet:

$order = Mage::getModel('sales/order')->loadByIncrementId($incrementID);

$creditmemo = Mage::getModel('sales/service_order', $order)
    ->prepareCreditMemo()
    ->register()
    ->save();

This approach uses Magento’s Sales_Service_Order model, which encapsulates the logic for creating and saving credit memos. The prepareCreditMemo() method handles all necessary calculations, while register() and save() finalize the process.

Proper Status Management

The original code also handles order status changes incorrectly:

$order->addStatusToHistory($orderCreditMemoStatusCode, $orderCreditMemoStatusComment, false);

This will not update the actual order status. Instead, you should use the following approach to reflect the changes properly:

$order->setState(Mage_Sales_Model_Order::STATE_CLOSED)->setStatus($orderCreditMemoStatusCode);
$order->save();

By calling setState() and setStatus(), you ensure that both the state and status of the order are updated to reflect the credit memo creation.

Conclusion

Creating a Magento credit memo programmatically requires leveraging the platform’s built-in functions designed for this purpose. The provided code snippet demonstrates how to use the Sales_Service_Order model correctly, ensuring accurate total calculations and proper status management.

Remember that Magento 2 provides extensive documentation on its API and models, which is your go-to resource for finding the right approaches to common tasks like this one.

If you’re looking to further optimize your Magento development processes or need expert guidance on other aspects of e-commerce platform management, don’t hesitate to check out Laravel Company. We offer a wide range of services tailored to help businesses thrive in the digital landscape.

Magento 2 Orders Pricing Errors

Our network