Magento2 自定义邮政编码的输入掩码

6.76K 浏览翻译

Magento2 自定义邮政编码的输入掩码

PS:如需转载,请留言,转载后请说明出处,否则虽繁必究!!!


这篇文章描述了开发人员如何添加自定义的输入掩码。

      在下单或者购物车页面上,当顾客在配送地址中指定国家和邮政编码时,Magento 会检查输入的邮政编码格式是否符合所指定的国家。用邮政编码这个字段的输入掩码来执行这个校验。在 Magento 中,这些输入掩码是定义了允许格式的正则表达式。
      在 Magento 中,邮政编码字段的输入掩码定义在<Magento_Directory_module_dir>/etc/zip_codes.xml。每个国家都用正则表达式定义了输入掩码。定于的语法查看  zip_code.xsd

下面这个表格定义了邮政编码的属性:
属性名称
                   
必填
                   
描述
                   
countryCode
                   

                   
代表哪个国家的邮政编码被定义
                   
<zip countryCode="US">
     <!-- Here we add the zip codes -->
</zip>

下面这个表格定义了编码的属性:

属性名称
                   
必填
                   
描述
                   
id
                   

                   
在同一个列表中,任意的唯一的名称
                   
example
                   

                   
允许格式的例子
                   
active
                   

                   
定义这个邮政格式是否启用
                   

你可以为同一个国家定义几个邮政编码的格式,通过传入一个编码列表:
<codes>
     <code id="pattern_1" active="true" example="12345">^[0-9]{5}$</code>
     <code id="pattern_2" active="true" example="AB1234">^[a-zA-z]{2}[0-9]{4}$</code>
</codes>

为了兼容性,可升级性和易维护性,不要编辑默认的 Magento 代码。在不同的自定义模块中添加你的自定义代码。为了你的邮政编码输入掩码正确地执行,你的自定义模块应该基于 Magento_Direcotry 模块。不要用 Ui 作为你的自定义模块的名字,因为当指定路径的时候, %Vendor%_Ui 这个符号可能会有问题。

添加自定义的邮政编码输入掩码
为添加自定义的或者改变默认的邮政编码的输入掩码,在 <你的模块目录>/etc 目录下新建一个 zip_code.xml 。
这个文件的内容应该跟下面这个例子相似:
<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Directory:etc/zip_codes.xsd">

	<!-- Specify the country ISO code-->
	<zip countryCode="US">
		<!-- You can specify several patterns for one country -->
    	<codes>
	        <code id="pattern_1" active="true" example="12345-6789">^[0-9]{5}-[0-9]{4}$</code>
    	    <code id="pattern_2" active="true" example="12345">^[0-9]{5}$</code>
    	</codes>
	</zip></config>

修改已有的掩码的默认值
为了改变(重写默认值)已存在的掩码:
  1. 打开 zip_codes.xml 。
  2. 复制相关节点。
  3. 改变定义掩码的正则表达式和相应的示例值。
改变默认输入掩码的示例:
在默认的 <Magento_Directory_module_dir>/etc/zip_codes.xml 中,下面这个掩码是为法国设置的:
<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Directory:etc/zip_codes.xsd">...	<zip countryCode="FR">
        <codes>
            <code id="pattern_1" active="true" example="12345">^[0-9]{5}$</code>
        </codes>
    </zip>...</config>
在你的 your zip_codes.xml 中,添加下面的代码来改变这个掩码:
<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Directory:etc/zip_codes.xsd">...	<zip countryCode="FR">
        <codes>
            <!-- Changed the regexp defining the mask, and the value of "example" -->
            <code id="pattern_1" active="true" example="A123">^[a-zA-Z]{1}[0-9]{3}$</code>
        </codes>
    </zip>...</config>

移除掩码
为了移除掩码,在你的 zip_codes.xml 中添加相应的节点,设置 <code/> 的 active 属性为 false 。

PS:如需转载,请留言,转载后请说明出处,否则虽繁必究!!!

4

写得很好,学习了。之前都不知道还有这种操作。

0

很细节的点,一波骚操作呀,哈哈哈.....给赞!!

0

谢谢~~,小妹开心到飞起

0

哎呀,被大佬关注到了,O(∩_∩)O

0