magento2 将地址的国家城市邮编等设置为非必填

Country 会影响address list 加载,当country为空时会报错

app/code/xxx/Customer/Model/ResourceModel/Country.php
app/code/xxx/Customer/etc/di.xml

di.xml
<preference for="Magento\Directory\Model\ResourceModel\Country" type="xxx\Customer\Model\ResourceModel\Country" />
Country.php
/**
     * Load country by ISO code
     *
     * @param \Magento\Directory\Model\Country $country
     * @param string $code
     * @return \Magento\Directory\Model\ResourceModel\Country
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function loadByCode(\Magento\Directory\Model\Country $country, $code)
    {
        switch (strlen($code)) {
            case 2:
                $field = 'iso2_code';
                break;

            case 3:
                $field = 'iso3_code';
                break;

            default:
                $field = 'iso2_code';
                break;
        }
        return $this->load($country, $code, $field);
    }

form和listting 隐藏国家city 等,postcode的验证比较复杂,直接给了一个00000默认值

app/code/xxx/Customer/view/adminhtml/ui_component/customer_address_form.xml
app/code/xxx/Customer/view/adminhtml/ui_component/customer_address_listing.xml

## 设置form 字段隐藏并设置默认值

<field name="postcode"  sortOrder="120"  formElement="hidden">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="default" xsi:type="string">00000</item>
                </item>
            </argument>
            <settings>
                <dataType>text</dataType>
                <visible>true</visible>
                <label translate="true">Zip/Postal Code</label>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">false</rule>
                </validation>
            </settings>
        </field>
## 设置list 字段隐藏
 
<column name="postcode" sortOrder="92">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="componentDisabled" xsi:type="boolean">true</item>
                </item>
            </argument>
            <settings>
                <filter>text</filter>
                <label translate="true">Zip/Postal Code</label>
            </settings>
        </column>

前端编辑页面, 直接屏蔽对应字段,postcode也是直接给了一个00000默认值

app/code/xxx/Customer/view/frontend/templates/address/edit.phtml

General 负责前端地址数据的验证,重新设定验证规则

app/code/xxx/Customer/Model/Address/Validator/General.php

   /**
     * @inheritdoc
     */
    public function validate(AbstractAddress $address)
    {
        return $this->checkRequiredFields($address);
    }

    /**
     * Check fields that are generally required.
     *
     * @param AbstractAddress $address
     * @return array
     * @throws \Zend_Validate_Exception
     */
    private function checkRequiredFields(AbstractAddress $address)
    {
        $errors = [];
        if (!\Zend_Validate::is($address->getFirstname(), 'NotEmpty')) {
            $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'firstname']);
        }

        if (!\Zend_Validate::is($address->getLastname(), 'NotEmpty')) {
            $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'lastname']);
        }

        if (!\Zend_Validate::is($address->getStreetLine(1), 'NotEmpty')) {
            $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'street']);
        }

        if (!\Zend_Validate::is($address->getCompany(), 'NotEmpty')) {
            $errors[] = __('"%fieldName" is required. Enter and try again.', ['fieldName' => 'company']);
        }

        return $errors;
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。