Magento – Add a field to a custom Payment method (frontend)

checkoutformslayoutpayment

I would like to add a field in the checkout page for a custom payment method.

Actually because my method extends CC the form is automatically generated (I have no layout set) look at -> Mage_Checkout_Block_Onepage_Payment_Methods with getPaymentMethodFormHtml() function:

    public function getPaymentMethodFormHtml(Mage_Payment_Model_Method_Abstract $method)
{
     return $this->getChildHtml('payment.method.' . $method->getCode());
}

This is good however I would like to add a comment form ( only for this payment method ) using a custom layout:
keep the current form and add an extra block with an text input field.

Best Answer

In your payment model, you should set protected $_formBlockType = 'foo/form'; class that derives from Mage_Payment_Model_Method_Cc and create a template/foo/form.phtml file. This allows you to write your own form file.

Then you need to override assignData($data) function (payment model) in order to save this extra field value in the $paymentInfo obj.

In case you will need to show that field in info as well you can do the same as above with $_infoBlockType variable.