Skip to content

Custom checkout field save input value in database – Magento 2.4

We found the default documentation about adding a new input field to the checkout. https://devdocs.magento.com/guides/v2.3/howdoi/checkout/checkout_form.html

enter image description here

I want to save input value in database with new table

How can we achieve that?

Thanks so much.

vendor/module/view/frontend/layout/checkout_index_index.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="checkout.root">
        <arguments>
            <argument name="jsLayout" xsi:type="array">
                <item name="components" xsi:type="array">
                    <item name="checkout" xsi:type="array">
                        <item name="children" xsi:type="array">
                            <item name="steps" xsi:type="array">
                                <item name="children" xsi:type="array">
                                    <item name="shipping-step" xsi:type="array">
                                        <item name="children" xsi:type="array">
                                            <item name="shippingAddress" xsi:type="array">
                                                <item name="children" xsi:type="array">
                                                    <item name="before-form" xsi:type="array">
                                                        <item name="children" xsi:type="array">
                                                            <!-- newsletter component -->
                                                            <item name="newsletter" xsi:type="array">
                                                                <item name="component" xsi:type="string">vendor_module/js/view/newsletter</item>
                                                                <item name="config" xsi:type="array">
                                                                    <item name="componentDisabled" xsi:type="boolean">true</item>
                                                                </item>
                                                            </item>
                                                            <!-- new form -->
                                                            <item name="custom-checkout-form-container" xsi:type="array">
                                                                <!--links to our js file in step 1-->
                                                                <item name="component" xsi:type="string">vendor_module/js/view/custom-checkout-form</item>
                                                                <item name="provider" xsi:type="string">checkoutProvider</item>
                                                                <item name="config" xsi:type="array">
                                                                    <!--links to our html file in step 2-->
                                                                    <item name="template" xsi:type="string">vendor_module/custom-checkout-form</item>
                                                                </item>
                                                                <!-- new element -->
                                                                <item name="children" xsi:type="array">
                                                                    <item name="custom-checkout-form-fieldset" xsi:type="array">
                                                                        <!-- uiComponent is used as a wrapper for form fields (its template will render all children as a list) -->
                                                                        <item name="component" xsi:type="string">uiComponent</item>
                                                                        <!-- the following display area is used in template (see below) -->
                                                                        <item name="displayArea" xsi:type="string">custom-checkout-form-fields</item>
                                                                        <item name="children" xsi:type="array">
                                                                            <item name="select_field" xsi:type="array">
                                                                                <item name="component" xsi:type="string">Magento_Ui/js/form/element/select</item>
                                                                                <item name="config" xsi:type="array">
                                                                                    <!--customScope is used to group elements within a single form (e.g. they can be validated separately)-->
                                                                                    <item name="customScope" xsi:type="string">customCheckoutForm</item>
                                                                                    <item name="template" xsi:type="string">ui/form/field</item>
                                                                                    <item name="elementTmpl" xsi:type="string">ui/form/element/select</item>
                                                                                </item>
                                                                                <item name="options" xsi:type="array">
                                                                                    <item name="0" xsi:type="array">
                                                                                        <item name="label" xsi:type="string" translate="true">Please select value</item>
                                                                                        <item name="value" xsi:type="string">value_0</item>
                                                                                    </item>
                                                                                    <item name="1" xsi:type="array">
                                                                                        <item name="label" xsi:type="string" translate="true">Reading</item>
                                                                                        <item name="value" xsi:type="string">value_1</item>
                                                                                    </item>
                                                                                    <item name="2" xsi:type="array">
                                                                                        <item name="label" xsi:type="string" translate="true">Swimming</item>
                                                                                        <item name="value" xsi:type="string">value_2</item>
                                                                                    </item>
                                                                                    <item name="3" xsi:type="array">
                                                                                        <item name="label" xsi:type="string" translate="true">Listen Music</item>
                                                                                        <item name="value" xsi:type="string">value_3</item>
                                                                                    </item>
                                                                                </item>
                                                                                <!-- value element allows to specify default value of the form field -->
                                                                                <item name="value" xsi:type="string">value_0</item>
                                                                                <item name="provider" xsi:type="string">checkoutProvider</item>
                                                                                <item name="dataScope" xsi:type="string">customCheckoutForm.select_field</item>
                                                                                <item name="label" xsi:type="string" translate="true">Hobbies</item>
                                                                                <item name="sortOrder" xsi:type="string">1</item>
                                                                            </item>
                                                                            <item name="select_field_2" xsi:type="array">
                                                                                <item name="component" xsi:type="string">Magento_Ui/js/form/element/select</item>
                                                                                <item name="config" xsi:type="array">
                                                                                    <!--customScope is used to group elements within a single form (e.g. they can be validated separately)-->
                                                                                    <item name="customScope" xsi:type="string">customCheckoutForm</item>
                                                                                    <item name="template" xsi:type="string">ui/form/field</item>
                                                                                    <item name="elementTmpl" xsi:type="string">ui/form/element/select</item>
                                                                                </item>
                                                                                <item name="options" xsi:type="array">
                                                                                    <item name="0" xsi:type="array">
                                                                                        <item name="label" xsi:type="string" translate="true">Please select value</item>
                                                                                        <item name="value" xsi:type="string">value_0</item>
                                                                                    </item>
                                                                                    <item name="1" xsi:type="array">
                                                                                        <item name="label" xsi:type="string" translate="true">Income 100$ ~ 200$/Week</item>
                                                                                        <item name="value" xsi:type="string">value_4</item>
                                                                                    </item>
                                                                                    <item name="2" xsi:type="array">
                                                                                        <item name="label" xsi:type="string" translate="true">Income 200$ ~ 300$/Week</item>
                                                                                        <item name="value" xsi:type="string">value_5</item>
                                                                                    </item>
                                                                                </item>
                                                                                <!-- value element allows to specify default value of the form field -->
                                                                                <item name="value" xsi:type="string">value_0</item>
                                                                                <item name="provider" xsi:type="string">checkoutProvider</item>
                                                                                <item name="dataScope" xsi:type="string">customCheckoutForm.select_field_2</item>
                                                                                <item name="label" xsi:type="string" translate="true">Income</item>
                                                                                <item name="sortOrder" xsi:type="string">2</item>
                                                                            </item>
                                                                        </item>
                                                                    </item>
                                                                </item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </item>
            </argument>
        </arguments>
    </referenceBlock>
</body>

vendor/module/view/frontend/web/js/view/custom-checkout-form.js

/*global define*/
define([
'Magento_Ui/js/form/form'
], function(Component) {
'use strict';
return Component.extend({
    initialize: function () {
        this._super();
        // component initialization logic
        return this;
    },

    /**
     * Form submit handler
     *
     * This method can have any name.
     */
    onSubmit: function() {
        // trigger form validation
        this.source.set('params.invalid', false);
        this.source.trigger('customCheckoutForm.data.validate');

        // verify that form data is valid
        if (!this.source.get('params.invalid')) {
            // data is retrieved from data provider by value of the customScope property
            var formData = this.source.get('customCheckoutForm');
            // do something with form data
            console.dir(formData);
        }
    }
});
});

<your_module_dir>/view/frontend/web/template/custom-checkout-form.html

<div>
<form id="custom-checkout-form" class="form" data-bind="attr: {'data-hasrequired': $t('* Required Fields')}">
    <fieldset class="fieldset">
        <legend data-bind="i18n: 'Custom Checkout Form'"></legend>
        <!-- ko foreach: getRegion('custom-checkout-form-fields') -->
        <!-- ko template: getTemplate() --><!-- /ko -->
        <!--/ko-->
    </fieldset>
    <button type="reset">
        <span data-bind="i18n: 'Reset'"></span>
    </button>
    <button type="button" data-bind="click: onSubmit" class="action">
        <span data-bind="i18n: 'Submit'"></span>
    </button>
</form>