Adding Custom Fields Block After Shipping Method in Magento2 Checkout
Adding custom fields after the shipping method in the Magento2 checkout process involves creating a custom module and configuring XML layout updates to place your block appropriately. In this guide, we’ll walk you through the steps required to achieve this.
Prerequisites
Before you begin, make sure you have a basic understanding of Magento2 development and that your environment is set up with PHP 7.x or later, MySQL 5.6 or later, and Composer installed on your local machine.
Creating the Custom Module
-
Start by creating a new directory for your custom module in
app/code. The directory should be named following Magento’s naming conventions, e.g.,Your_Company/Your_Module. -
Inside this directory, create the necessary files and directories:
- app/code
- Your_Company
- Your_Module
- etc
- module.xml
- view
- frontend
- layout
your_module_custom_fields.xml
- templates
custom_fields.phtml
Configuring the Custom Fields Block
Open app/code/Your_Company/Your_Module/etc/module.xml and add the following content to enable your module:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Moduledetector/Schema/etc/module_xsd">
<module name="Your_Company_Your_Module" setup_version="1.0.0">
<sequence>
<module name="Magento_Checkout"/>
</sequence>
</module>
</config>
Next, open app/code/Your_Company/Your_Module/view/frontend/layout/your_module_custom_fields.xml and add the following code to create your custom fields block:
<?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>
<referenceBlock name="checkout.root">
<block class="Magento\Framework\View\Element\Template" template="Your_Company_Your_Module::custom_fields.phtml" name="your_module_custom_fields_block" cacheable="false"/>
</referenceBlock>
</body>
</page>
Creating the Custom Fields Template
Create a new file app/code/Your_Company/Your_Module/view/frontend/templates/custom_fields.phtml and add your custom fields markup, for example:
<div id="your-company-custom-fields">
<label for="field1">Field 1</label>
<input type="text" name="field1" id="field1" class="input-text">
<!-- Add more custom fields as needed -->
</div>
<script>
require(['jquery'], function($) {
$('#checkout-shipping-method-load').on('show.mage', function() {
$('#your-company-custom-fields').show();
});
$('#checkout-shipping-method-load').on('hide.mage', function() {
$('#your-company-custom-fields').hide();
});
});
</script>
In this example, we’re using jQuery to show and hide the custom fields block based on whether any shipping method is selected.
Enabling Your Custom Module
To enable your module in Magento2:
- Run
bin/magento setup:upgradefrom your terminal. - Clear the cache by running
bin/mageo cache:flush.
Now, when you access the checkout page and select a shipping method, your custom fields block should appear after the shipping method list.
Conclusion
Adding custom fields after the shipping method in Magento2 involves creating a new module, defining an XML layout update to place the custom block, and creating the necessary HTML template for displaying your custom fields. By following this guide, you can easily extend the checkout functionality to meet your specific business requirements.
Remember that when dealing with checkout modifications, it’s essential to follow best practices and thoroughly test your changes to ensure they don’t negatively impact other parts of the checkout process or introduce security vulnerabilities.
Don’t forget to explore more Magento2 development resources available at Laravel Company, where you can find in-depth tutorials, code examples, and expert advice on various aspects of e-commerce development.