Magento – How to create custom drop down field on checkout page in Magento 2

checkoutdrop-downsmagento2template

How to create custom field on checkout billing address in magento2?

I want simple drop down on checkout page just we can use for admin order and this field also saves in the database table like order table and b/c I will use in advertisement.

Best Answer

For Frontend:

In layout file checkout_index_index call the template file which has your dropdown html code.it will be displayed on you checkout page.

Code: file name has checkout_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="checkout" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
             <block class="Test\CommunityCommerce\Block\Charity\Charity" name="charity_charity" after="checkout.cart.coupon" template="Sundial_CommunityCommerce::checkout/cart/charity.phtml">
            </block>
        </referenceContainer>
</body>

In Template file consists :

file name has charity.phtml

    <div class="content" data-role="content" aria-labelledby="block-charity-heading">
        <form id="charity-form"
              action="#"
              method="post"
              value="0" />
            <?php //echo $block->getBlockHtml('formkey') ?>
                <div class="field">
                    <div class="control">
                         <select id="charity_id" name="charity_id">                     
                                <option value="test1">test1</option>
                                <option value="test2">test2</option>
                          </select>
                    </div>
                </div>

        </form>         
    </div>      
</div>

Create column in quote table and sales table using setup scripts using "InstallSchema" it will create column into table.

After that using sales_convert_quote and fieldset.xml it will move to quote value to sales table

Related Topic