Logo

How to remove postcode field from Magento2 checkout

In order to accomplish the postcode removal we need to:

  1. Remove it from checkout form
  2. Disable the postcode validators

1. Remove it from checkout form: Alter the postcode eav attribute (set is_user_defined to 1)

The postcode field is created by the vendor/magento/module-checkout/Block/Checkout/LayoutProcessor.php block using the customer_address  and customer_register_address EAV attributes. If we set the postcode EAV attribute property is_user_defined to 1, then it won’t be included into the jsLayout json object based on which the checkout form is generated.

Let’s begin: the first step is to create a new module with a setup data resource which will change this attribute, let’s call it Bb_RemovePostcode.

a) Create the    app/code/Bb/RemovePostcode/etc/module.xml:

b) Create the app/code/Bb/RemovePostcode/registration.php  :

c) Create the  app/code/Bb/RemovePostcode/Setup/InstallData.php setup file:

Let’s activate the module by running on the cli on the root magento2 folder the following command: php bin/magento module:enable Fsu_RemovePostcode
You should see a green message.

And then:  php bin/magento setup:upgrade
You should see a green message.

At this moment you should be able to see that the postcode checkout field was removed from the shipping address form.

You’ll be unable to complete an order because some postcode validators are still enabled. Let’s disable them.

2. Disabling the postcode validators

a) first validator you’ll find on the customer address repository object,  vendor\magento\module-customer\Model\ResourceModel\AddressRepository.php:284 and for disabling it we need to overwrite this class.
– let’s create the di.xml file under etc/ folder:  app/code/Bb/RemovePostcode/etc/di.xml :

– and the   app/code/Bb/RemovePostcode/Model/ResourceModel/AddressRepository.php class file with the contents:

We can see here that we extended both save() and _validate() methods in order to remove the postcode validator.

b) Another validator the we need to remove is located under the  Magento\Customer\Model\Address\Validator\Postcode class. Let’s overwrite isValid method with the help of a Plugin:
– open the di.xml file created at previous step and let’s add the following node:

– now let’s create the Plugin class, specified in the type attribute above  Bb\RemovePostcode\Plugin\Customer\Address\PostcodeValidator :

It’s a very simple plugin that overwrites the returned value to boolean true no matter what the previous value was. So now this validator will return true no matter what.

c) Similar to the previous approach we need o extend the  Magento\Customer\Model\Attribute\Data\Postcode ‘s validate() method. We will do it via another Plugin:
– open the di.xml file created at previous step and let’s add the following node:

– create the class here:  Bb\RemovePostcode\Plugin\Customer\Address\PostcodeValidatorValue :

The overwritten validate() method has many other validators for the Customer Address. This method we added after it’s execution via the Plugin’s afterMethod feature it’s checking if the validate() method returns just one error and if that’s error cause is the empty postcode field. If so then ignore this and return true.

d) The last one, also similar to the previous validator is located on  Magento\Customer\Model\Address\AbstractAddress  class, validateValue() method. We will extend it’s behaviour it via another Plugin:
– open the di.xml file created at previous step and let’s add the following node:

– and let’s create the Bb\RemovePostcode\Plugin\Customer\Address\PostcodeValidatorAddress  class:

Pretty self explanatory, returns no error regardless the previous validating result.

 

At this point you should be able to place a new order successfully, without the postcode field.

Let me know in the comments if it worked.

Cheers.

Back to blog


Leave A Comment