How To

Magento 2: How to Add Placeholder Text in Field for Checkout Page

Hello Magento Friends,

Today we will learn to Add Placeholder Text in Field for Checkout Page in Magento 2.

If you want to display a hint or example of the input required in a particular field, you can add placeholder text. With default Magento 2, there is no placeholder text in fields at the checkout page. To add placeholder text in the field for the checkout page in Magento 2, use the below steps.

Steps to Add Placeholder Text in Field for Checkout Page in Magento 2:

Step 1: First create di.xml file at the given below path

app/code/Vendor/Extension/etc/frontend/di.xml

Now add the code as follows

<?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\AttributeMerger">
          <pluginname="add_placeholderto_checkout" type="Vendor\Extension\Plugin\Block\Checkout\AttributeMerger" sortOrder="10"/>
     </type>
</config>

Step 2: Now create a Plugin File at the following path

app/code/Vendor/Extension/Plugin/ Block/ Checkout/ AttributeMerger.php

Then add the following code

<?php
namespace Vendor\Extension\Plugin\Block\Checkout;

class AttributeMerger
{
     public function afterMerge(\Magento\Checkout\Block\Checkout\AttributeMerger $subject,$result)
     {
          $result['firstname']['placeholder'] = __('First Name');
          $result['lastname']['placeholder'] = __('Last Name');
          $result['street']['children'][0]['placeholder'] = __('Line no 1');
          $result['street']['children'][1]['placeholder'] = __('Line no 2');
          $result['city']['placeholder'] = __('Enter City');
          $result['postcode']['placeholder'] = __('Enter Zip/Postal Code');
          $result['telephone']['placeholder'] = __('Enter Phone Number');
          return $result;
      }
}

At last, clear the cache and check the output on your checkout page.

Conclusion:

This way you can easily add placeholder text in various fields of the Magento 2 checkout page. If you are unable to get the desired results, share them with me and I will quickly solve your problem. Share the tutorial with your friends to let them customize the checkout page.

Happy Coding!

Click to rate this post!
[Total: 1 Average: 5]
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.?

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago