Magento 2 – Override Directory Module /etc XML File

magento-2.1magento2xml

I am trying to override the ZIP code validation xml file in Magento's Directory module. I'm doing this because I want to enforce a space in UK postcodes. The path of the file I'm attempting to override is:

/vendor/magento/module-directory/etc/zip_codes.xml

I'm attempting to override this by creating a file in my theme with the same name:

/app/design/frontend/[Company]/[Theme]/Magento_Directory/etc/zip_codes.xml

This hasn't had the desired effect, I presume I'm missing a key step but I've not been able to figure out what else is required. Any help would be greatly appreciated.

Best Answer

You cannot overwrite an existing XML file from the etc directory. What you are trying to do is working for files in the view directory only.

What you can do is extend an existing XML file with your own module which will be loaded after the Magento_Directory module (controlled by the <sequence> tag in the module.xml). This module can have a etc/zip_codes.xml file too. Don't copy all of the original file, just the header and the footer:

<?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">
</config>

So let's assume you want to change the zip range for Australia (AU) which is as follows by default:

<zip countryCode="AU">
    <codes>
        <code id="pattern_1" active="true" example="1234">^[0-9]{4}$</code>
    </codes>
</zip>

You can add a second range in your own module:

<zip countryCode="AU">
    <codes>
        <code id="pattern_2" active="true" example="12345">^[0-9]{5}$</code>
    </codes>
</zip>

Or disable an existing pattern:

<zip countryCode="AU">
    <codes>
        <code id="pattern_1" active="false" />
    </codes>
</zip>

Or change an existing entry:

<zip countryCode="AU">
    <codes>
        <code id="pattern_1" active="true" example="12345">^[0-9]{5}$</code>
    </codes>
</zip>

You can access an existing entry by using the same main identifier id.

For your case the zip_codes.xml will probably look as follows (+ removed):

<?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="GB">
        <codes>
            <code id="pattern_1" active="true" example="AB12 3CD">^[a-zA-Z]{2}[0-9]{2}\s[0-9]{1}[a-zA-Z]{2}$</code>
            <code id="pattern_2" active="true" example="A1B 2CD">^[a-zA-Z]{1}[0-9]{1}[a-zA-Z]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
            <code id="pattern_3" active="true" example="AB1 2CD">^[a-zA-Z]{2}[0-9]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
            <code id="pattern_4" active="true" example="AB1C 2DF">^[a-zA-Z]{2}[0-9]{1}[a-zA-Z]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
            <code id="pattern_5" active="true" example="A12 3BC">^[a-zA-Z]{1}[0-9]{2}\s[0-9]{1}[a-zA-Z]{2}$</code>
            <code id="pattern_6" active="true" example="A1 2BC">^[a-zA-Z]{1}[0-9]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
        </codes>
    </zip>
</config>
Related Topic