Add a custom form to Magento 2 checkout.
Add a custom form to Magento 2 checkout.
Adds a configurable custom form to the Magento 2 checkout (shipping step), placed above the shipping-method selection.
The form works for both logged-in customers and guests.
After an order is placed, all entered data is stored in the sales_order table and exposed via extension attributes on OrderInterface.
Form data persists through page refreshes as long as the cart is active.
Fields can be individually enabled or disabled per store view from the admin panel.
Optional character-length limits can be configured for each field.
| Field | Config key |
|---|---|
| Buyer name | checkout_buyer_name |
| Buyer email | checkout_buyer_email |
| Purchase order no. | checkout_purchase_order_no |
| Goods mark | checkout_goods_mark |
| Comment | checkout_comment |
| Method | Endpoint | Auth |
|---|---|---|
PUT |
/V1/carts/mine/set-order-custom-fields |
Customer |
PUT |
/V1/guest-carts/:cartId/set-order-custom-field |
Guest |
| Module version | Magento | PHP |
|---|---|---|
2.0.* |
2.4.6 – 2.4.8 | 8.1 · 8.2 · 8.3 · 8.4 |
1.2.* |
2.3.x | 7.x |
1.1.* |
2.1.x – 2.2.x | (no longer supported) |
composer require sbodak/magento2-checkout-custom-form
php bin/magento module:enable Bodak_CheckoutCustomForm
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:flushGo to Stores → Configuration → Sales → Checkout → Checkout Custom Form Configuration.
Api/Data/CustomFieldsInterface.php and the ATTRIBUTES array.Setup/Patch/Data/AddCustomFields.php.Model/Data/CustomFields.php with getters, setters, and isXxxEnabled().Model/Config/Source/Option.php.Model/Checkout/LayoutProcessor/Plugin.php ($fields array).Observer/AddCustomFieldsToOrder.php if custom mapping is required.Observer/Sales/OrderLoadAfter.php to populate the extension attribute.view/adminhtml/templates/order/view/custom_fields.phtml
and view/frontend/templates/order/view/custom_fields.phtml.In Model/Checkout/LayoutProcessor/Plugin.php, add a validation key to the field array:
[
'dataScopeName' => CustomFieldsInterface::CHECKOUT_PURCHASE_ORDER_NO,
'label' => 'Purchase order no.',
'validation' => ['required-entry' => true],
],Edit i18n/en_US.csv (or create a language-specific CSV, e.g. de_DE.csv).
composer install
vendor/bin/phpunitphp bin/magento module:uninstall Bodak_CheckoutCustomFormThis drops the custom columns from the sales_order and quote tables.
See CHANGELOG.md.
No open issues yet, or sync has not completed.