Changing Billing Address Position in Magento 2
If you’re looking to change the default position of the billing address field in your Magento 2 store, you’ve come to the right place. In this guide, we will walk you through the process of moving the billing address section from under payment information to after shipping method selection.
Understanding the Current Setup
By default, Magento 2 places the billing address form just below the payment methods step in the checkout process. This is located within the payment block in the onepage.phtml file.
<block class="Magento\Checkout\Block\Onepage" name="checkoutsteps.payment.form">
<arguments>
<argument name="show_paragraphs" xsi:type="string">false</argument>
</arguments>
....
</block>
Moving the Billing Address Section
To change this position, we need to modify the checkout template and move the billing address form into a separate block. We can do this by following these steps:
-
Create a new template file: Navigate to your theme directory (or create one under
app/design/frontend/{Vendor}/{Theme}/template) and look for thecheckoutfolder. If it doesn’t exist, create it. -
Duplicate the existing onepage.phtml file: Copy the
onepage.phtmlfile from the Magento_Checkout module (vendor/magento/module-checkout/view/frontend/templates/onepage.phtml) to your theme’scheckoutfolder. -
Edit the duplicated template file: Open the copied
onepage.phtmlin a text editor. This is where we will make our changes. -
Move the billing address form into a new block: Find the line of code that starts with
<block class="Magento\Checkout\Block\Onepage" name="checkoutsteps.payment.form">. Cut and paste this entire block, including its opening and closing tags, but place it just above the closing</div>tag for theshipping_methodblock.
<block class="Magento\Checkout\Block\Onepage" name="billing-address-form">
<arguments>
<argument name="show_paragraphs" xsi:type="string">false</argument>
</arguments>
....
</block>
- Update the step titles: Make sure to update the step title in your theme to reflect this change, if needed.
Conclusion
Changing the position of the billing address section to after the shipping method selection requires a careful modification of the checkout template file in your custom theme. Always remember to create a backup before making any changes and test thoroughly on a staging server before deploying to production.
For more advanced customizations and best practices, consider checking out our guide on Advanced Customization Techniques in Magento 2. It provides insightful tips from experienced developers like yourself.