当前位置: 技术文章>> Magento 2:如何在结帐页面的字段中添加占位符文本

文章标题:Magento 2:如何在结帐页面的字段中添加占位符文本
  • 文章分类: Magento
  • 18345 阅读
系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》

本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。


在Magento 2中结帐页面字段中添加占位符文本的步骤:

步骤1:首先 在给定的以下路径创建di.xml文件

app/code/vendor/extension/etc/frontend/di.xml

现在添加代码,如下所示

<?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>

第 2 步:现在在以下路径创建一个插件文件

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

然后添加以下代码

<?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;
      }
}

最后,清除缓存并检查结帐页面上的输出。

结论:

这样,您可以轻松地在Magento 2结帐页面的各个字段中添加占位符文本。


推荐文章