How To

How to Auto filled Checkout shipping address form fields in Magento 2

People just love their convenience, whether it is shopping or anything and we are all lazy to some extent, if someone asks for the same or common information repeatedly. In this digital era, where no one has time and that’s the reason why people order online, whether it’s food, clothes or anything instead of visiting the nearby shop. Among available online shops, some online stores have a lengthy online store registration process that leads to higher cart abandonment. That’s the reason why several store owner has implemented a mobile login facility or has a simplified store shopping process as a part of customer convenience.

Recently, we came across one of our client’s requirement where he wants to auto-complete checkout shipping address fields. As developer several times you came across a requirement where you needs to auto filled after or before your checkout is loaded. Because auto filling some of the common shipping address fields like country, state, city etc saves the time of your customers and let them quickly checkout store products.

Firstly, we need to create “di.xml” file inside your extension etc folder and paste below code inside the file.
app\code\vendor\extension\etc\di.xml

<pre class="lang:default decode:true">
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Block\Checkout\LayoutProcessor">
        <plugin name="vendor_extension_default_value" type="Vendor\Extension\Plugin\LayoutProcessor" sortOrder="100"/>
    </type>
</config>
</pre>

After that, we need to create one more file “LayoutProcessor.php” file inside your extension folder using below code.

app\code\Vendor\Extension\Plugin\LayoutProcessor.php

<pre class="lang:default decode:true">
<?php
namespace Vendor\Extension\Plugin;
class LayoutProcessor
{
    public function afterProcess(\Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
        array  $jsLayout
    
    
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['firstname']['value'] = 'Test';
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['lastname']['value'] = 'Test';
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['company']['value'] = 'Vendor';
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['city']['value'] = 'Bhavnagar';
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['postcode']['value'] = '364002';
        $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['telephone']['value'] = '911234567890';
        return $jsLayout;
    
}
</pre>

That’s it! Now whenever your customer loads checkout in the frontend, your shipping address fields automatically gets filled.

If you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends.

And, Let us know if you are facing an issue while implementing this code.

Happy Filling!

Click to rate this post!
[Total: 15 Average: 4.3]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

Recent Posts

How to Add a Custom Field to the Cart Price Rules Form in Magento 2?

Hello Magento Friends, In this blog, we will learn How to Add a Custom Field…

11 minutes ago

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

3 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

5 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

7 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago