skip to Main Content

I want to add a new field job number after email in checkout form magento 2.enter image description here

2

Answers


  1. You could create a new block and template in a custom module and place it into the checkout page. Here is an example that you can put in checkout_index_index.xml. You would create this checkout_index_index.xml inside your new module.

    <?xml version="1.0"?>
      <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
            <referenceContainer name="checkout.root">
             <block name="job.custom" template="CustomModule::job/job_field.phtml" class="CustomModuleBlockJob" before="-"></block>
            </referenceContainer>
        </body>
      </page>
    

    Depending on where you want to put the field, you can use the before or after tag accordingly.

    Login or Signup to reply.
  2. app/code/Vendor/Module/Setup/UpgradeSchema.php

    <?php
    
    namespace VendorModuleSetup;
    
    use MagentoFrameworkSetupUpgradeSchemaInterface;
    use MagentoFrameworkSetupSchemaSetupInterface;
    use MagentoFrameworkSetupModuleContextInterface;
    
    class UpgradeSchema implements UpgradeSchemaInterface
    {
        /**
         * Upgrades DB schema for a module
         *
         * @param SchemaSetupInterface $setup
         * @param ModuleContextInterface $context
         * @return void
         */
        public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
        {
            $setup->startSetup();
    
            $quoteAddressTable = 'quote';
            $orderTable = 'sales_order';
    
            //Quote address table
            $setup->getConnection()
                ->addColumn(
                    $setup->getTable($quoteAddressTable),
                    'custom_test',
                    [
                        'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
                        'length' => 255,
                        'comment' =>'Custom Test'
                    ]
                );
            //Order address table
            $setup->getConnection()
                ->addColumn(
                    $setup->getTable($orderTable),
                    'custom_test',
                    [
                        'type' => MagentoFrameworkDBDdlTable::TYPE_TEXT,
                        'length' => 255,
                        'comment' =>'Custom Test'
    
                    ]
                );
    
            $setup->endSetup();
        }
    }
    

    Add our custom field to checkout page app/code/Vendor/Module/view/frontend/layout/checkout_index_index.xml

    <?xml version="1.0"?>
    <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="shipping-address-fieldset" xsi:type="array">
                                                                <!--Our custom test fieldset here-->
                                                                <item name="children" xsi:type="array">
                                                                    <item name="customer_test" xsi:type="array">
                                                                        <item name="sortOrder" xsi:type="string">300</item>
                                                                    </item>
                                                                </item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </argument>
                </arguments>
            </referenceBlock>
        </body>
    </page>
    

    Create fieldset.xml

    app/code/Vendor/Module/etc/fieldset.xml

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="urn:magento:framework:DataObject/etc/fieldset.xsd">
        <scope id="global">
            <fieldset id="sales_convert_quote">
                <field name="custom_test">
                    <aspect name="to_order" />
                </field>
            </fieldset>
        </scope>
    </config>
    

    Don’t forget to create registration.php and etc/module.xml

    app/code/Vendor/Module/registration.php

    <?php
    MagentoFrameworkComponentComponentRegistrar::register(
        MagentoFrameworkComponentComponentRegistrar::MODULE,
        'Vendor_Module',
        __DIR__
    );
    

    app/code/Vendor/Module/etc/module.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="Vendor_Module" setup_version="1.0.0"/>
    </config>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search