Magento – Magento 2.3.2 Address How to Remove City Field

checkoutcitymagento2.3.2onepage-checkout

I am using the Magento 2.3.2.

I want to remove "City" field into the checkout.

I have added the following code into my theme checkout_index_index.xml file:

<item name="city" xsi:type="array">
  <item name="visible" xsi:type="boolean">false</item>
</item>

The City field is not displaying But when I am Placing the order then following error occured:

Please check the shipping address information. "city" is required. Enter and try again.

Can you please tell me how to remove the city field into the checkout without error when placing an order.

Thanks

Best Answer

Remove Postcode from Checkout

Beacuse the php validation return false, inside class Magento\Quote\Model\Quote\Address you will find validate 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();
        if (!in_array(
            $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.');
        }
       if (empty($errors) || $this->getShouldIgnoreValidation()) {
            return true;
        }
        return $errors;
      }
}

commented the code related to city field

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

Update :-

Execute This Query in Database :-

SELECT * FROM `eav_attribute` WHERE `attribute_code` LIKE 'city' ORDER BY `attribute_id` DESC

And Check is_requred is 0 or 1

If 1 Than Change And Try