2

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.

  • Please add your code that you have to try override zipcode xml – Kamlesh Solanki Apr 24 '18 at 12:45
  • I have simply tried cloning the file from and to the locations specified, and changing a single word to check to see if the new version is being used. This is not the case, so I believe the problem is the file location or my method, rather than a code issue. – M Hayward Apr 24 '18 at 13:07
0

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>
  • 1
    Thanks for your answer Andreas, you've definitely explained well where I went wrong, and what should be done. After following this methodology, which I also found explained here. I ran in to the issue described here - which still seems to be a problem in Magento 2.1.12. I've used a workaround described in this issue which has resolved my problem. – M Hayward Apr 25 '18 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.