TransWikia.com

Magento 2 : Remove the city field in checkout billing address

Magento Asked by Dalia Abu Ghoush on November 20, 2021

How to remove the city field in checkout billing address, I tried removing it from checkout_index_index.xml but it’s not working?

Magento ver. 2.3.5-p1

2 Answers

app/design/frontend/theme/themefolerder/Magento_Checkout/layout /checkout_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.root">
            <arguments>
                <argument name="jsLayout" xsi:type="array">
                    <item name="components" xsi:type="array">
                        <item name="checkout" xsi:type="array">
                            <item name="children" xsi:type="array">
                                <item name="steps" xsi:type="array">
                                    <item name="children" xsi:type="array">
                                        <item name="shipping-step" xsi:type="array">
                                            <item name="children" xsi:type="array">
                                                <item name="shippingAddress" xsi:type="array">
                                                    <item name="children" xsi:type="array">
                                                        <item name="shipping-address-fieldset" xsi:type="array">
                                                            <item name="children" xsi:type="array">
                                                                <item name="city" xsi:type="array">
                                                                    <item name="visible" xsi:type="boolean">false</item>
                                                                    <item name="disabled" xsi:type="string">disabled</item>
                                                                    <item name="value" xsi:type="string">Value</item>
                                                                </item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

Answered by usman on November 20, 2021

add the following code into your theme checkout_index_index.xml file:

    <?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
  <referenceBlock name="checkout.root">
    <arguments>
    <argument name="jsLayout" xsi:type="array">
        <item name="components" xsi:type="array">
            <item name="checkout" xsi:type="array">
                <item name="children" xsi:type="array">
                    <item name="steps" xsi:type="array">
                        <item name="children" xsi:type="array">                                 

                             <item name="billing-step" xsi:type="array">
                                <item name="children" xsi:type="array">
                                    <item name="billingAddress" xsi:type="array">
                                        <item name="children" xsi:type="array">
                                             <!-- The name of the form the field belongs to -->
                                                <item name="billing -address-fieldset" xsi:type="array">
                                                    <item name="children" xsi:type="array">
                                                        <!--Remove fields-->
                                                        <item name="city" xsi:type="array">
                                                            <item name="visible" xsi:type="boolean">false</item>
                                                        </item>
                                                    </item>
                                                </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                            <item name="shipping-step" xsi:type="array">
                                <item name="children" xsi:type="array">
                                    <item name="shippingAddress" xsi:type="array">
                                        <item name="children" xsi:type="array">
                                            <!-- The name of the form the field belongs to -->
                                            <item name="shipping-address-fieldset" xsi:type="array">
                                                <item name="children" xsi:type="array">
                                                    <!--Remove fields-->
                                                    <item name="city" xsi:type="array">
                                                        <item name="visible" xsi:type="boolean">false</item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </item>
            </item>
        </item>
    </argument>
   </arguments>
  </referenceBlock>
 </body>
</page>

after using this code, city field will remove but getting error on placing order. Error: an error occured on server. Please try again to order place.

Beacuse the php validation return false, inside class MagentoQuoteModelQuoteAddress you will find validate function :

First create the di.xml inisde you extension directly in folder etc YourVendor/YourExtName/etc/di.xml and fill : Remove Postcode from Checkout

Beacuse the php validation return false, inside class MagentoQuoteModelQuoteAddress 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="MagentoQuoteModelQuoteAddress" type="YourVendorYourExtNameModelQuoteAddress" />
</config>

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

<?php
namespace YourVendorYourExtNameModelQuote;
class Address extends MagentoQuoteModelQuoteAddress
{
   /**
     * 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

Answered by Sanaullah Ahmad on November 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP