Magento - Sending Order Refund Notifications
Magento provides robust order management features out of the box, including the ability to process refunds for orders. In this post, we’ll guide you through sending an email notification to customers when their order is refunded and updating the order status accordingly.
Sending Email Notifications for Refunded Orders
To send a custom email notification when an order is refunded in Magento 1.7.0.2, follow these steps:
Step 1: Create a Custom Email Template
First, create a new email template under System > Transactional Emails. Name the template “Refund Notification” and add your desired content.
Step 2: Override the core_order_refund Observer
Next, you’ll need to override the core_order_refund observer. Create a new module if you don’t have one already, following Magento’s best practices for creating modules.
Step 3: Add an Observer
Add the following code to your config.xml file:
<config>
<global>
<events>
<sales_order_refund_save_after>
<observers>
<your_module_model>
<class>Your_Module_Model_Observer</class>
<method>sendRefundNotification</method>
</your_module_model>
</observers>
</sales_order_refund_save_after>
</events>
</global>
</config>
Replace your_module with the actual name of your module.
Step 4: Implement the Observer
Create a new file app/code/local/Your/Module/Model/Observer.php:
class Your_Module_Model_Observer
{
public function sendRefundNotification($observer)
{
$order = $observer->getEvent()->getOrder();
if ($order->getState() == 'refunded') {
$templateId = Mage::getStoreConfig('trans_email/ident_sales');
$sendTo = array($order->getCustomerEmail());
$vars = array(
'order' => $order
);
$mailer = Mage::getModel('core/email');
$mailer->setSender(Mage::storeConfig('trans_email/ident_general/name'), Mage::getStoreConfig('trans_email/ident_general/email'));
$mailer->setTemplateId($templateId);
$mailer->setTemplateParams($vars);
$mailer->setRecipients($sendTo);
$mailer->addAttachment(Mage::helper('sales')->prepareInvoicePdf($order, true));
$mailer->setSubject('Your Order Has Been Refunded');
$mailer->send();
}
}
}
This observer will fire whenever an order’s state changes to ‘refunded’, sending the specified email template and any attached invoices.
Updating the Order Status
To automatically update the order status when a refund is processed, you can add the following code in your module:
Step 1: Define a New Order Status
Add the following array to app/code/core/Mage/Sales/Model/Config.php:
$orderStatuses = array(
...
'refunded' => array('label' => 'Refunded', 'status' => Mage_Sales_Model_Order::STATE_PROCESSING),
);
Step 2: Update the Observer
In your Observer.php file, add this code to change the order status after sending the notification:
if ($order->getState() == 'refunded') {
// Send email...
$order->setState('refunded');
$order->save();
}
Conclusion
In Magento 1.7.0.2, you can effectively manage order refunds by sending automated notifications to customers and updating the order status accordingly. By following these best practices, you maintain a seamless customer experience and ensure transparency throughout the order lifecycle.
Remember to always test your changes thoroughly before deploying them to production environments. This will help prevent any potential issues or disruptions in your Magento store’s operations.
For more advanced customizations and tips on Magento development, feel free to explore our comprehensive guides at Laravel Company.