How to auto-fill customer address in shipping form at checkout page Magento2 – Today i am going to write some thing we commonly need in Magento 2 projects. Which is auto-fill customer address field where some fields already have values. Have values may be stored in cookies or session etc.
This is something little bit tricky and pretty simple task to achieve. You can achieve this with simple custom module using Plugin. I assume we already have a Custom Module here. Apart from creating a Custom Module we need to add 2 more extra steps.
- Creating di.xml file
- Creating the Plugin.
Creating di.xml file ( Ayakil/SetValue/etc/di.xml )
1 2 3 4 5 6 7 |
<?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="ayakil_setvalue_default_value" type="Ayakil\SetValue\Plugin\Checkout\Model\Checkout\LayoutProcessor" sortOrder="150"/> </type> </config> |
Creating the Plugin.
Path of the plugin is – app/code/Ayakil/SetValue/Plugin/Checkout/Model/Checkout/LayoutProcessor.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php namespace Ayakil\SetValue\Plugin\Checkout\Model\Checkout; class LayoutProcessor { /** * @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject * @param array $jsLayout * @return array */ 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'] = 'Mujahidh'; $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children'] ['shippingAddress']['children']['shipping-address-fieldset']['children']['lastname']['value'] = 'Haseem'; $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children'] ['shippingAddress']['children']['shipping-address-fieldset']['children']['company']['value'] = 'Ayakil'; $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children'] ['shippingAddress']['children']['shipping-address-fieldset']['children']['city']['value'] = 'Colombo'; //Here i added a condition to check the cookie is set with the zipcode and assigned the already set cookie. if (isset($_COOKIE['zip_code'])) { $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children'] ['shippingAddress']['children']['shipping-address-fieldset']['children']['postcode']['value'] = $_COOKIE['zip_code']; } $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children'] ['shippingAddress']['children']['shipping-address-fieldset']['children']['telephone']['value'] = '094769819117'; return $jsLayout; } } |
That’s it. How to auto-fill customer address in shipping form at checkout page Magento2 is explained. Now you can see these default values in checkout page both guest and logged users first time.
You can views more article on GraphQl written by me.
That’s it. Have a nice day.Enjoy coding , Learn , Experience , Teach and Help.
1 thought on “How to auto-fill customer address fields in shipping form at checkout page Magento2 ?”