Adding Custom Fields to Saved Credit Card Payment Method in Magento
Adding custom fields to the saved credit card payment method in Magento requires a combination of configuration file modifications and database schema updates. In this tutorial, we’ll guide you through the process of adding a ‘parcelas’ select field with 1 to x options.
Step 1: Modify the Configuration File
Open your app/code/core/Mage/Payment/etc/config.xml file in a text editor. Locate the <payment><ccsave> section and add the following lines inside it:
<fieldset>
<label>Parcelas</label>
<field id="parcelas" type="select">
<source_model>adminhtml/system_config_source_yesno</source_model>
<required>1</required>
<sort_order>90</sort_order>
<options>
<option value="1">1</option>
<option value="2">2</option>
<!-- Add more options as needed -->
</options>
</field>
</fieldset>
This code adds a new field called ‘parcelas’ to the saved credit card configuration page. Remember to backup your original file before making any changes.
Step 2: Update the Database Schema
Next, we need to update the database schema to store these custom fields. Open your app/code/core/Mage/Core/sql/payment Setup/install-0.7.4.sql file and add the following SQL statement at the end:
CREATE TABLE IF NOT EXISTS `{$this->getTable('sales/quote')}` (
`entity_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`parent_id` INT(11),
PRIMARY KEY (`entity_id`),
FOREIGN KEY (`parent_id`) REFERENCES `{$this->getTable('core/resource')}`
);
This statement creates a new table named ‘sales_quote’ to store the data related to payment methods.
Step 3: Get and Store Data from the Form
To get the data from the form, you need to create an observer that listens for the event payment_method_is_active. Create a new file called app/code/core/Mage/Payment/Model/Observer.php with the following content:
class Mage_Payment_Model_Observer
{
public function handlePaymentMethodIsActive($event)
{
$quote = $event->getQuote();
$parcelas = $quote->getData('payment_method_instance')->getParcelas();
// Store 'parcelas' data somewhere, for example in a custom table
}
}
In the handlePaymentMethodIsActive function, you can access the ‘parcelas’ field using $quote->getData('payment_method_instance')->getParcelas(). You would then store this value in your custom database table.
Step 4: Retrieve Data from the Backend
To retrieve data from the backend, you need to create a new admin grid that displays the saved credit card payment methods along with their ‘parcelas’ values. This involves creating a new module and following Magento’s standard for creating admin grids.
Remember to clear your Magento cache after implementing these changes:
php -f shell/indexer.php reindexall
php -f shell/compiler.php disable
Conclusion
Adding custom fields to the saved credit card payment method in Magento requires a good understanding of configuration files, database schema updates, and event listeners. By following this guide, you should be able to add your ‘parcelas’ field successfully.
For more advanced tutorials on Magento development, consider visiting LaravelCompany. We offer a wide range of resources for developers looking to master the Magento platform.