user900
user900

Reputation: 1

Remove fields in the checkout page in Prestashop

Im trying to remove some fields form the checkout page (specifically: postcode, city, address 2)

First was a problem to remove the city field, I went to: International / Locations them I selected country and when I tried to remove the field "city" I couldn't, PrestaShop said the following message:

"The city field (in tab Address) is required"

So after some google, I found out I need to modify: classes/Address.php

I commented the whole line 134 that said:

'city' => ['type' => self::TYPE_STRING, 'validate' => 'isCityName', 'required' => true, 'size' => 64],

The problem persisted, and I still got the same message, so I edit the file classes/AddressFormat.php

I commented the line 63 that said "city":

   /** @var array Default required form fields list */
    public static $requireFormFieldsList = [
        'firstname',
        'lastname',
        'address1',
        // 'city',
        'Country:name',
    ];

Now, I finally was available to remove the field. But the issue still persit. Let me explain my self:

If you go to the checkout page, you can see the postal code and city field.

Screenshot - Address field default

But, when I select the country, everything looks like is supposed to look.

Screenshot - Address After selecting country

Any idea? How can I do, so i can remove the postcode and the city field?

Btw, my cache is off, but, I had cleaned my cache (in the performance page) and also I cleaned the cache on my browser. Also, I deleted this folder: /var/cache/prod/

Cache disabled Field selected

Thanks guys

I use Prestashop: 1.7.7.2

Upvotes: 0

Views: 3159

Answers (1)

Crezzur
Crezzur

Reputation: 1481

For this to happen you just need to alter one file. The file we alter is a core file so make sure to make a backup before you start editing. We will make a filter which checks if the value for example postcode is in the filter. When the value is in the filter it will not create a form-field anymore.

  1. Open file Yourstore/classes/form/CustomerAddressFormatter.php.
  2. Search for public function getFormat() and navigate to the section foreach ($fields as $field) {

Below foreach ($fields as $field) { we add

    // In this case the fields postcode, adress1 and address2 will be removed from the Addresses form
    $ignorefields = ['postcode', 'address1', 'address2'];
    if (!in_array($field, $ignorefields)) {

Then we keep scrolling down until we find $format[$formField->getName()] = $formField;. Under this line we add a bracket } to close the function.

So all together you will now have a new function like this:

    foreach ($fields as $field) {
    // Add whatever value you which to hide to the array $ignorefields to remove it from the Addresses form-field.
    // Available values: address1, address2, postcode, city, State, phone, phone_mobile, company, vat_number
    $ignorefields = ['postcode', 'address1', 'address2'];
    if (!in_array($field, $ignorefields)) {
        $formField = new FormField();
        $formField->setName($field);
        $fieldParts = explode(':', $field, 2);
        if (count($fieldParts) === 1) {
            if ($field === 'postcode') {
                if ($this->country->need_zip_code) {
                    $formField->setRequired(true);
                }
            } elseif ($field === 'phone') {
                $formField->setType('tel');
            } elseif ($field === 'dni' && null !== $this->country) {
                if ($this->country->need_identification_number) {
                    $formField->setRequired(true);
                }
            }
        } elseif (count($fieldParts) === 2) {
            list($entity, $entityField) = $fieldParts;
            $formField->setType('select');
            $formField->setName('id_' . strtolower($entity));
            if ($entity === 'Country') {
                $formField->setType('countrySelect');
                $formField->setValue($this->country->id);
                foreach ($this->availableCountries as $country) {
                    $formField->addAvailableValue(
                        $country['id_country'],
                        $country[$entityField]
                    );
                }
            } elseif ($entity === 'State') {
                if ($this->country->contains_states) {
                    $states = State::getStatesByIdCountry($this->country->id, true);
                    foreach ($states as $state) {
                        $formField->addAvailableValue(
                            $state['id_state'],
                            $state[$entityField]
                        );
                    }
                    $formField->setRequired(true);
                }
            }
        }
        $formField->setLabel($this->getFieldLabel($field));
        if (!$formField->isRequired()) {
            $formField->setRequired(
                array_key_exists($field, $required)
            );
        }
        $format[$formField->getName()] = $formField;
    } // Add bracket to close our new function - Crezzur
}

Upvotes: 2

Related Questions