/   /   /  Show Hide Fields (ex : street , city , region , country ….) in Checkout page

Note:

For more extensions and themes visit our store

Show Hide Fields (ex : street , city , region , country ….) in Checkout page


The checkout page contain standard fields , in many cases the site owner needs to hide some fields for specific reason , the system give you the ability to hide some fields directy from xml , you can add checkout_index_index.xml (copy same structure or just what you want from this path vendor/magento/module-checkout/view/frontend/layout/checkout_index_index.xml) to you theme or your custom extension and start hiding some fields .

Hide Zip Code :
Go to file and search for postcode and add or override tag :

  1. <item name="visible" xsi:type="boolean">false</item>
  2.  

Complete tag code :
  1. <item name="postcode" xsi:type="array">
  2.    <item name="visible" xsi:type="boolean">false</item>
  3.         <item name="component" xsi:type="string">Magento_Ui/js/form/element/post-code</item>
  4.               <item name="validation" xsi:type="array">
  5.                   <item name="required-entry" xsi:type="string">true</item>
  6.                </item>
  7.    </item>
  8.  

Hide Company :
 
  1. <item name="company" xsi:type="array">
  2.      <item name="visible" xsi:type="boolean">false</item>  
  3.             <item name="validation" xsi:type="array">
  4.                <item name="min_text_length" xsi:type="number">0</item>
  5.             </item>
  6. </item>
  7.  

The same case for some fields (and the data will saved in db without backend validation) , but for  city , region , country , street , firstname and alst name it's have specific backend validation with php …..

Hide City Case :
First the same thing in checkout_index_index.xml :

  1. <item name="city" xsi:type="array">
  2.        <item name="visible" xsi:type="boolean">false</item>
  3. </item>
  4.  
 
But when you try to submit the system return as result an error , beacuse the php validation return false, inside class Magento\Quote\Model\Quote\Address you will find validate function : 
   
  1. public function validate()
  2.     {
  3.         $errors = [];
  4.         if (!\Zend_Validate::is($this->getFirstname(), 'NotEmpty')) {
  5.             $errors[] = __('Please enter the first name.');
  6.         }
  7.         if (!\Zend_Validate::is($this->getLastname(), 'NotEmpty')) {
  8.             $errors[] = __('Please enter the last name.');
  9.         }
  10.         if (!\Zend_Validate::is($this->getStreetLine(1), 'NotEmpty')) {
  11.             $errors[] = __('Please enter the street.');
  12.         }
  13.         if (!\Zend_Validate::is($this->getCity(), 'NotEmpty')) {
  14.            $errors[] = __('Please enter the city.');
  15.         }
  16.          if (!\Zend_Validate::is($this->getTelephone(), 'NotEmpty')) {
  17.             $errors[] = __('Please enter the phone number.');
  18.         }
  19.         $_havingOptionalZip = $this->_directoryData->getCountriesWithOptionalZip();
  20.         if (!in_array(
  21.             $this->getCountryId(),
  22.             $_havingOptionalZip
  23.         ) && !\Zend_Validate::is(
  24.             $this->getPostcode(),
  25.             'NotEmpty'
  26.         )
  27.         ) {
  28.             $errors[] = __('Please enter the zip/postal code.');
  29.         }
  30.         if (!\Zend_Validate::is($this->getCountryId(), 'NotEmpty')) {
  31.             $errors[] = __('Please enter the country.');
  32.         }
  33.         if ($this->getCountryModel()->getRegionCollection()->getSize() && !\Zend_Validate::is(
  34.             $this->getRegionId(),
  35.             'NotEmpty'
  36.         ) && $this->_directoryData->isRegionRequired(
  37.             $this->getCountryId()
  38.         )
  39.         ) {
  40.             $errors[] = __('Please enter the state/province.');
  41.         }
  42.         if (empty($errors) || $this->getShouldIgnoreValidation()) {
  43.             return true;
  44.         }
  45.         return $errors;
  46.     }
  47.  

Let's start  override the function . first create the di,xml inisde you extension directly in folder etc YourVendor/YourExtName/etc/di.xml  and fill :

  1. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  2.     <preference for="Magento\Quote\Model\Quote\Address" type="YourVendor\YourExtName\Model\Quote\Address" />
  3. </config>
  4.  

Now create the class  Address inside YourVendor/YourExtName/Model/Qoute/Adress,php and push :

  1. <?php
  2. namespace YourVendor\YourExtName\Model\Quote;
  3. class Address extends \Magento\Quote\Model\Quote\Address
  4. {
  5.    /**
  6.      * Validate address attribute values
  7.      *
  8.      * @return bool|array
  9.      * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  10.      * @SuppressWarnings(PHPMD.NPathComplexity)
  11.      */
  12.     public function validate()
  13.     {
  14.         $errors = [];
  15.         if (!\Zend_Validate::is($this->getFirstname(), 'NotEmpty')) {
  16.             $errors[] = __('Please enter the first name.');
  17.         }
  18.        if (!\Zend_Validate::is($this->getLastname(), 'NotEmpty')) {
  19.             $errors[] = __('Please enter the last name.');
  20.         }
  21.         if (!\Zend_Validate::is($this->getStreetLine(1), 'NotEmpty')) {
  22.             $errors[] = __('Please enter the street.');
  23.         }
  24.          if (!\Zend_Validate::is($this->getCity(), 'NotEmpty')) {
  25.             //$errors[] = __('Please enter the city.');
  26.         }
  27.         if (!\Zend_Validate::is($this->getTelephone(), 'NotEmpty')) {
  28.             $errors[] = __('Please enter the phone number.');
  29.         }
  30.         $_havingOptionalZip = $this->_directoryData->getCountriesWithOptionalZip();
  31.         if (!in_array(
  32.             $this->getCountryId(),
  33.             $_havingOptionalZip
  34.         ) && !\Zend_Validate::is(
  35.             $this->getPostcode(),
  36.             'NotEmpty'
  37.         )
  38.         ) {
  39.             $errors[] = __('Please enter the zip/postal code.');
  40.         }
  41.          if (!\Zend_Validate::is($this->getCountryId(), 'NotEmpty')) {
  42.             $errors[] = __('Please enter the country.');
  43.         }
  44.         if ($this->getCountryModel()->getRegionCollection()->getSize() && !\Zend_Validate::is(
  45.             $this->getRegionId(),
  46.             'NotEmpty'
  47.         ) && $this->_directoryData->isRegionRequired(
  48.             $this->getCountryId()
  49.         )
  50.         ) {
  51.             $errors[] = __('Please enter the state/province.');
  52.         }
  53.        if (empty($errors) || $this->getShouldIgnoreValidation()) {
  54.             return true;
  55.         }
  56.         return $errors;
  57.       }
  58. }
  59.  

In Our Example we have commented the code related to city test : 
       

  1. if (!\Zend_Validate::is($this->getCity(), 'NotEmpty')) {
  2.             //$errors[] = __('Please enter the city.');
  3.         }
  4.  

Finally if you test now to submit the order , you will have the success page without errors and system skip the backend validation of city …..

Comments

Related Posts

make your store more efficient

Solving problems. With open source technology. Professional results. That’s what makes Ibnab your best choice

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.

Newsletter