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 :
<item name="visible" xsi:type="boolean">false</item>
Complete tag code :
<item name="postcode" xsi:type="array"> <item name="visible" xsi:type="boolean">false</item> <item name="component" xsi:type="string">Magento_Ui/js/form/element/post-code</item> <item name="validation" xsi:type="array"> <item name="required-entry" xsi:type="string">true</item> </item> </item>
Hide Company :
<item name="company" xsi:type="array"> <item name="visible" xsi:type="boolean">false</item> <item name="validation" xsi:type="array"> <item name="min_text_length" xsi:type="number">0</item> </item> </item>
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 :
<item name="city" xsi:type="array"> <item name="visible" xsi:type="boolean">false</item> </item>
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 :
public function validate() { $errors = []; if (!\Zend_Validate::is($this->getFirstname(), 'NotEmpty')) { $errors[] = __('Please enter the first name.'); } if (!\Zend_Validate::is($this->getLastname(), 'NotEmpty')) { $errors[] = __('Please enter the last name.'); } if (!\Zend_Validate::is($this->getStreetLine(1), 'NotEmpty')) { $errors[] = __('Please enter the street.'); } if (!\Zend_Validate::is($this->getCity(), 'NotEmpty')) { $errors[] = __('Please enter the city.'); } if (!\Zend_Validate::is($this->getTelephone(), 'NotEmpty')) { $errors[] = __('Please enter the phone number.'); } $_havingOptionalZip = $this->_directoryData->getCountriesWithOptionalZip(); $this->getCountryId(), $_havingOptionalZip ) && !\Zend_Validate::is( $this->getPostcode(), 'NotEmpty' ) ) { $errors[] = __('Please enter the zip/postal code.'); } if (!\Zend_Validate::is($this->getCountryId(), 'NotEmpty')) { $errors[] = __('Please enter the country.'); } if ($this->getCountryModel()->getRegionCollection()->getSize() && !\Zend_Validate::is( $this->getRegionId(), 'NotEmpty' ) && $this->_directoryData->isRegionRequired( $this->getCountryId() ) ) { $errors[] = __('Please enter the state/province.'); } return true; } return $errors; }
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 :
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Quote\Model\Quote\Address" type="YourVendor\YourExtName\Model\Quote\Address" /> </config>
Now create the class Address inside YourVendor/YourExtName/Model/Qoute/Adress,php and push :
<?php namespace YourVendor\YourExtName\Model\Quote; class Address extends \Magento\Quote\Model\Quote\Address { /** * Validate address attribute values * * @return bool|array * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function validate() { $errors = []; if (!\Zend_Validate::is($this->getFirstname(), 'NotEmpty')) { $errors[] = __('Please enter the first name.'); } if (!\Zend_Validate::is($this->getLastname(), 'NotEmpty')) { $errors[] = __('Please enter the last name.'); } if (!\Zend_Validate::is($this->getStreetLine(1), 'NotEmpty')) { $errors[] = __('Please enter the street.'); } if (!\Zend_Validate::is($this->getCity(), 'NotEmpty')) { //$errors[] = __('Please enter the city.'); } if (!\Zend_Validate::is($this->getTelephone(), 'NotEmpty')) { $errors[] = __('Please enter the phone number.'); } $_havingOptionalZip = $this->_directoryData->getCountriesWithOptionalZip(); $this->getCountryId(), $_havingOptionalZip ) && !\Zend_Validate::is( $this->getPostcode(), 'NotEmpty' ) ) { $errors[] = __('Please enter the zip/postal code.'); } if (!\Zend_Validate::is($this->getCountryId(), 'NotEmpty')) { $errors[] = __('Please enter the country.'); } if ($this->getCountryModel()->getRegionCollection()->getSize() && !\Zend_Validate::is( $this->getRegionId(), 'NotEmpty' ) && $this->_directoryData->isRegionRequired( $this->getCountryId() ) ) { $errors[] = __('Please enter the state/province.'); } return true; } return $errors; } }
In Our Example we have commented the code related to city test :
if (!\Zend_Validate::is($this->getCity(), 'NotEmpty')) { //$errors[] = __('Please enter the city.'); }
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