Magento - Show Whether Item Has Been Returned to Stock on Credit Memo
When processing credit memos in Magento, you sometimes need to refund purchased products and automatically add them back to stock. However, once the credit memo is created, it only shows whether the product has been refunded but not necessarily returned to stock. This tutorial guides you through creating a custom extension that displays this information.
The Problem
The existing Magento functionality lacks clear indication of which items were actually added back to stock when processing a credit memo. You can see if an item was refunded, but not whether it was returned to the inventory.
The Solution: A Custom Extension
To solve this issue, we’ll create an extension that adds a new attribute ‘returned_to_stock’ to the sales_flat_creditmemo_item table and updates its value during credit memo creation.
Step 1: Add a New Attribute
In your install script (usually located in your extension’s app/code/local/Vendor/Module/sql Vendor_Module Setup/install-0.1.0.sql), add the following code to create the new attribute:
CREATE TABLE IF NOT EXISTS sales_flat_creditmemo_item (
entity_id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
parent_id INT(11) UNSIGNED NOT NULL DEFAULT '0',
creditmemoid INT(11) UNSIGNED NOT NULL DEFAULT '0',
product_id INT(11) UNSIGNED NOT NULL DEFAULT '0',
parent_item_id INT(11) UNSIGNED DEFAULT NULL,
order_qty DECIMAL(12,4) NOT NULL DEFAULT '0.0000',
returned_to_stock TINYINT(1) unsigned DEFAULT 0,
PRI KEY (entity_id),
INDEX parent_item_id (parent_item_id)
);
Step 2: Set Up an Observer
Next, create an observer that triggers on the sales_order_creditmemo_save_after event. In your extension’s config.xml, add the following:
<events>
<sales_order_creditmemo_save_after>
<observers>
<vendor_module_refundobserver>
<type>model</type>
<class>Vendor_Module_Model_Observer</class>
<method>refundOrderInventory</method>
</vendor_module_refundobserver>
</observers>
</sales_order_creditmemo_save_after>
</events>
Step 3: Implement the Observer
Create a new file app/code/local/Vendor/Module/Model/Observer.php and add the following code:
<?php
class Vendor_Module_Model_Observer {
public function refundOrderInventory($observer) {
$creditmemo = $observer->getEvent()->getCreditmemo();
foreach ($creditmemo->getAllItems() as $item) {
if ($item->hasBackToStock() && $item->getBackToStock() && $item->getQty()) {
$item->setReturnedToStock(1);
$item->save();
}
}
}
}
Step 4: Update the Database
In your install script, add the following code to create the new column in the sales_flat_creditmemo_item table:
ALTER TABLE sales_flat_creditmemo_item ADD returned_to_stock TINYINT(1) unsigned DEFAULT 0;
How It Works
The observer is triggered whenever a credit memo is saved. It iterates through all items in the credit memo and checks if they were marked to be added back to stock.
If an item meets the criteria, it sets the returned_to_stock attribute to 1 using $item->setReturnedToStock(1); and saves the changes with $item->save();.
Conclusion
This custom extension provides a straightforward solution for tracking whether items were returned to stock during credit memo processing in Magento. By adding a new database field and updating it via an observer, you can easily display this information.
Remember to replace ‘Vendor’ and ‘Module’ in the code examples with your actual vendor name and module name.
For more advanced customizations or if you require professional assistance, feel free to contact us at Laravel Company. We specialize in Magento development and can help you implement complex extensions like this one.