1

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

4
  • Why not give the city a default value when saving it on the backend, such as placeholder, so that you can verify it correctly with magento without changing the code.
    – Tokey Tu
    Commented Aug 20, 2019 at 6:35
  • 1
    Hello @tokey, Nice Idea.Can you please tell me How to set CIty default value. Commented Aug 20, 2019 at 7:44
  • you can preference Magento\Checkout\Controller\Onepage\SaveOrder,or use sales_order_save_before event save custom city default value.
    – Tokey Tu
    Commented Aug 20, 2019 at 8:05
  • 1
    Not working it throws the error first City is required because I think it is checked validation via the jquery Commented Aug 20, 2019 at 9:18

2 Answers 2

4

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

12
  • 1
    when the customer is logged in then not working.for guest only working Commented Aug 19, 2019 at 13:02
  • try to clean cache and static content deploy Commented Aug 19, 2019 at 13:07
  • 1
    Yes, I have do this step Commented Aug 19, 2019 at 13:09
  • 1
    Hello @RkRathod, I have tried many times but it is not working for a Logged in users. The city is required field error occurred. It is working great for the guest user. Commented Aug 20, 2019 at 5:32
  • 1
    I have removed the require field into the DB still same error occurs.I have checked with content:deploy.I think this is handle via the knockout js Commented Aug 20, 2019 at 7:28
2

Try changing xml to

<item name="city" xsi:type="array">
  <item name="visible" xsi:type="boolean">false</item>
  <item name="validation" xsi:type="array">
     <item name="required-entry" xsi:type="boolean">false</item>
   </item>
</item>
1
  • 1
    Same error occured Commented Aug 19, 2019 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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