Magento – Magento-2 : How to display custom field just after payment method list and save on place order

checkoutmagento2sales-order

I want to display one text area, just after payment method list, on checkout page.

  1. Text area value should be saved on place order.
  2. Saved value also need to be displayed in Admin >> sales >> order >> view >> payment method.

In which table should I add the custom columns, so the values can be easily displayed on the admin-side.

UPDATE

I have displayed text-area to checkout step, How to save value on place order?

Thanks

Best Answer

At first, you must not add any custom columns to existing core tables, if you need you should create own tables.

I see at least two possible solutions:

  1. It might be enough to store all custom fields in payment additional information, how to do it see dev docs. You can add custom fields to payment method additional_data before place order request, or read data from the request in the observer. After that, you need to add paymentInfoKeys for all existing payment methods (a plugin might be useful) - http://devdocs.magento.com/guides/v2.1/payments-integrations/base-integration/payment-option-config.html. Now, all custom fields will be displayed in payment method details.
  2. Create own table for custom fields and use extension attributes to store custom fields for payment method. In that case, you also need to retrieve custom data in the observer but store it in the extension attributes, as an example - https://github.com/magento/magento2/blob/develop/app/code/Magento/Vault/Observer/AfterPaymentSaveObserver.php (in your case it will retrieve data from the request or additional_data, store in custom table and set to the extension attributes) and you need an additional plugin to load extension attributes - https://github.com/magento/magento2/blob/develop/app/code/Magento/Vault/Plugin/PaymentVaultAttributesLoad.php (reads from custom table and set to extension attributes). The final step is to create a block and provide own layout to render custom data.

    <referenceBlock name="order_additional_info"> <block class="BlockClassName" name="custom_name" template="BlockClassName::template_name.phtml"/> </referenceBlock>

    The order_additonal_info will be available starting from 2.2.0 release but you can use payment_additional_info or order_payment_additional.

How to save value on place order?

Again, I see at least few solutions:

  1. Each payment method has additional_data and you can update it before place order but you need to prevent default placeOrder function for each payment method - https://github.com/magento/magento2/blob/develop/app/code/Magento/Checkout/view/frontend/web/js/view/payment/default.js#L129.
  2. Use afterPlaceOrder (https://github.com/magento/magento2/blob/develop/app/code/Magento/Checkout/view/frontend/web/js/view/payment/default.js#L48) callback to send own request with custom fields.
Related Topic