Adding Custom Payment Information in Magento Admin
When working with Magento 1.7.0.2, customizing the payment information view for an order in the admin panel requires careful consideration to avoid unwanted side effects. In this guide, we’ll demonstrate how to add a custom block containing credit card expiration date information without overriding core files or affecting other areas of the site.
Understanding Magento’s Payment Block Structure
Magento uses the Mage_Payment_Block_Info_Cc class for displaying credit card payment information. However, directly extending this class would apply changes across all instances where it is usedâincluding email templates and customer view pages.
To avoid this, we’ll create a new block that extends Mage_Core_Block_Template and use event observers to inject our custom content into the “Payment Information” section of the order view.
Step 1: Create a Custom Block
First, let’s create a new block file in your theme or module structure:
app/code/local/YourCompany/PaymentInfo/etc/config.xml
In this XML configuration file, define your custom block:
<config>
<global>
<blocks>
<yourcompany_paymentinfo>
<class>YourCompany_PaymentInfo_Block</class>
</yourcompany_paymentinfo>
<yourcompany_paymentinfo>
<block>
<cc_info>
<type>yourcompany_paymentinfo/cc_info</type>
<class>YourCompany_PaymentInfo_Block_Cc_Info</class>
</cc_info>
</block>
</yourcompany_paymentinfo>
</blocks>
</global>
</config>
Now, create the actual block file:
app/code/local/YourCompany/PaymentInfo/Block/Cc/Info.php
The Cc_Info class should extend Mage_Core_Block_Template:
class YourCompany_PaymentInfo_Block_Cc_Info extends Mage_Core_Block_Template
{
protected function _toHtml()
{
$order = $this->getOrder();
if ($paymentMethod == 'cc') {
$expirationDate = $order->getPayment()->getCcExpMonth() . '/' . $order->getPayment()->getCcExpYear();
return '<p><strong>Expiration Date:</strong> ' . $expirationDate . '</p>';
}
return '';
}
}
Step 2: Inject the Custom Block
To display this custom block in the “Payment Information” section, we need to create an event observer. This will inject our new block into the existing payment information layout.
Create a new XML configuration file:
app/code/local/YourCompany/PaymentInfo/etc/events.xml
In this file, define a new event observer to inject your custom block:
<config>
<global>
<events>
<sales_order_view_info>
<observers>
<yourcompany_paymentinfo_observer>
<type>singleton</type>
<class>YourCompany_PaymentInfo_Model_Observer</class>
<method>addCustomPaymentInfoBlock</method>
</yourcompany_paymentinfo_observer>
</observers>
</sales_order_view_info>
</events>
</global>
</config>
Now, create the observer method:
app/code/local/YourCompany/PaymentInfo/Model/Observer.php
Implement the addCustomPaymentInfoBlock method to inject your custom block into the layout:
class YourCompany_PaymentInfo_Model_Observer
{
public function addCustomPaymentInfoBlock(Varien_Event_Observer $observer)
{
$layout = $observer->getLayout();
$order = $observer->getOrder();
if ($paymentMethod == 'cc') {
$block = Mage::app()->getLayout()->createBlock(
'yourcompany_paymentinfo/cc_info'
)->setOrder($order);
$layout->getChild('sales_order_info')->insert(
$block,
'yourcompany.custom_info',
10
);
}
}
}
Step 3: Clear Magento Cache
After adding these changes, make sure to clear the Magento cache:
rm -rf var/cache/*
Now, when you view an order in the admin panel with a credit card payment method, your custom “Expiration Date” information should be displayed under the “Payment Information” section.
Conclusion
By following this approach, we successfully added a custom block to the “Payment Information” section of an order view in Magento 1.7.0.2 without overriding core files or affecting other areas of the siteâensuring our changes are localized and clean.
Remember, for any complex modifications like these, it’s always recommended to use a dedicated module rather than modifying your theme directly. This keeps your code organized, maintainable, and independent of theme updates.
For more advanced Magento customization techniques, consider exploring our comprehensive guides at Laravel Company.
This approach provides a robust solution for adding custom payment information to the admin order view while keeping your code modular and clean.