Adding Custom Fields to Magento Payment Module
Adding custom fields to an existing payment module in Magento requires careful modification of several components within your theme or extension. In this guide, we’ll walk through the necessary steps to add two new text boxes to the ‘Excellence Pay’ payment method.
Understanding the Current Code Structure
Let’s first understand what you’ve attempted so far and where things might have gone wrong:
1. Block/Info File Modification
You’ve correctly added your custom fields (check_field and check_field1) in the _prepareSpecificInformation() function of the block file:
class Excellence_Pay_Block_Info_Pay extends Mage_Payment_Block_Info
{
// ... existing code ...
protected function _prepareSpecificInformation($transport = null)
{
// ... existing code ...
$transport->addData(array(
'Voucher #' => $info->getCheckNo(),
'Voucher Date' => $info->getCheckDate(),
'Field Date' => $info->getCheckField(), //custom field
'Field new' => $info->getCheckField1() //custom field
));
return $transport;
}
}
However, this modification only affects the display of these fields on the payment information page after a successful order. It doesn’t change how the data is submitted or stored.
2. Model/Pay File Modification
You’ve also updated your assignData() function in the model file to accept and assign values for your new fields:
public function assignData($data)
{
// ... existing code ...
$info->setCheckField($data->getCheckField())
->setCheckField1($data->getCheckField1());
return $this;
}
This ensures that when the payment method is saved, these custom fields will be populated with the provided values. But again, this doesn’t change how the data is collected or processed.
3. Fieldsets Configuration in Config/XML
You’ve added your new fields to the fieldsets section of your module’s configuration XML:
<fieldsets>
<sales_convert_quote_payment>
<!-- ... existing fieldsets ... -->
<check_field>
<to_order_payment>*</to_order_payment>
</check_field>
<check_field1>
<to_order_payment>*</to_order_payment>
</check_field1>
</sales_convert_quote_payment>
</fieldsets>
This tells Magento to include these fields when converting a quote payment into an order payment. However, this doesn’t handle the actual data submission or validation.
The Missing Pieces
The issue lies in how you’re trying to collect and validate the data for your new fields.
1. Template File Modification
To actually display the new text boxes on the frontend, you need to modify the frontend template file (usually pay.phtml) where the payment information is rendered:
<div class="payment-method">
<!-- ... existing HTML ... -->
<div class="field field-check_field required-entry">
<label for="check_field"><?php echo $this->__('Field Date') ?>:<span class="required">*</span></label>
<input type="text" name="additional_information[check_field]" id="check_field" value="<?php echo $this->escapeHtml($info->getCheckField()) ?>" class="input-text required-entry validate-required">
</div>
<div class="field field-check_field1 required-entry">
<label for="check_field1"><?php echo $this->__('Field new') ?>:<span class="required">*</span></label>
<input type="text" name="additional_information[check_field1]" id="check_field1" value="<?php echo $this->escapeHtml($info->getCheckField1()) ?>" class="input-text required-entry validate-required">
</div>
</div>
This adds the two new text boxes with appropriate labels and validation attributes.
2. Additional Information Collection
To ensure that the data from these fields is correctly collected and passed to your payment method, you need to update how Magento handles additional information for the order:
In your pay model file, you should also modify the getAdditionalInformationKey() function:
public function getAdditionalInformationKey()
{
return 'additional_information';
}
This tells Magento where to look for the values submitted from these custom fields.
3. Data Validation and Processing
Finally, you need to handle the validation and processing of this additional information within your pay model file:
public function validate()
{
// ... existing code ...
$paymentData = $this->getInfoInstance()->getAdditionalInformation();
if (!isset($paymentData['check_field']) || !isset($paymentData['check_field1'])) {
Mage::throwException(Mage::helper('payment')->__('Invalid payment data'));
}
// Process and validate your custom fields here
return $this;
}
This ensures that Magento checks for the existence of these custom fields before proceeding with the payment process. You can add additional validation logic inside the if statement as needed.
Conclusion
Adding custom fields to a Magento payment module requires careful coordination across multiple files and components. By modifying the relevant block, model, configuration, template, and information collection methods, you can successfully add new text boxes to your ‘Excellence Pay’ payment method.
Remember that it’s crucial to test your changes thoroughly after making any modifications to ensure they don’t introduce unforeseen issues or conflicts with other parts of your Magento installation.