Extending Magento Credit Memo for Returned Items
Introduction
In this post, we’ll explore how to customize the Magento credit memo page to store information about whether an item was returned to stock or not. This involves creating an observer and extending the existing attributes of a credit memo product.
Identifying the Observer
You’ve already identified the correct observer in app/code/core/Mage/CatalogInventory/Model/Observer.php, specifically the refundOrderInventory() method. This is triggered when the admin submits a credit memo with the ‘Return to Stock’ checkbox checked, making it an ideal place to add your custom logic.
However, you’re having trouble figuring out how to add an extra attribute to the credit memo product items. Let’s walk through the process step by step.
Creating a Custom Attribute
First, create a new module if you haven’t already. In app/code/local, create a new folder with your vendor name and module name (e.g., Local_Mymodule). Inside this folder, create an etc directory containing a config.xml file:
<config>
<modules>
<Local_Mymodule>
<version>1.0.0</version>
</Local_Mymodule>
</modules>
<global>
<helpers>
<mymodule>
<class>Mymodule_Helper</class>
</mymodule>
</helpers>
</global>
</config>
Next, in app/code/local/Mymodule/Helper/Data.php, create a helper:
class Mymodule_Helper_Data extends Mage_Core_Helper_Abstract
{
}
Creating the Observer
Now, let’s create our observer. In app/code/local/Mymodule/Model/Observer.php:
class Mymodule_Model_Observer
{
public function creditMemoSaveAfter($observer)
{
$event = $observer->getEvent();
$creditmemo = $event->getCreditmemo();
// Assuming the 'return_item' column is boolean (1 for yes, 0 for no)
foreach ($creditmemo->getAllItems() as $item) {
if ($item->getReturnedToStock()) {
$item->setReturnItem(1);
} else {
$item->setReturnItem(0);
}
}
return;
}
}
Configuring the Observer
In app/code/local/Mymodule/etc/config.xml, add the following observer configuration:
<config>
<modules>
<!-- ... -->
</modules>
<global>
<helpers>
<!-- ... -->
</helpers>
<events>
<sales_creditmemo_save_after>
<observers>
<local_mymodule_save_after>
<class>mymodule/observer</class>
<method>creditMemoSaveAfter</method>
</local_mymodule_save_after>
</observers>
</sales_creditmemo_save_after>
</events>
</global>
</config>
Updating the Credit Memo View
You’ve already identified that you can add an extra column to app/design/adminhtml/default/default/template/sales/order/creditmemo/view/items.phtml and items/renderer/default.phtml. However, you need to make this attribute writeable and readable.
To do this, create a new field in your credit memo item collection. In app/code/local/Mymodule/Model/Creditmemo.php:
class Mymodule_Model_Creditmemo extends Mage_Sales_Model_Order_Creditmemo_Item
{
protected function _construct()
{
$this->_init('sales.OrderCreditmemoItem');
}
public function addFieldsToFilter(array $filter)
{
parent::addFieldsToFilter($filter);
$filter[] = 'main_table.return_item';
return $this;
}
}
Conclusion
In this post, we’ve walked through the process of customizing Magento credit memos to store whether items were returned to stock or not. By creating a new module and observer, along with modifying the existing codebase, you can achieve your desired functionality.
Remember, when working with Magento extensions, always follow best practices for maintainability and compatibility. Consider reaching out to Laravel Company if you need professional assistance with larger projects or complex customizations.
Happy coding!