Jump to content

Trying to add custom input on adress form


Recommended Posts

So I am trying to add a number field to adress form. This is becaus Brazilian norm of adress writing begin with street name, then the house number.

Ex: Rua Dom Pedro, 133

Due to it, people usally forget to add, number unless it has a separeted number field. Very comon on brazilian ecommerce.

Follows a print of what i am trying. If anyone has an idea please share.
Thank you for your time.

forms_17_2.jpg

Link to comment
Share on other sites

Prestashop 1.7.0.0 >

1. edit ./classes/CustomerAddressForm.php => validate() function

before:

public function validate()
    {
        $is_valid = true;

        $postcode = $this->getField('postcode');
        if ($postcode && $postcode->isRequired()) {
            $country = $this->formatter->getCountry();
            if (!$country->checkZipCode($postcode->getValue())) {
                $postcode->addError($this->translator->trans(
                    'Invalid postcode - should look like "%zipcode%"',
                    ['%zipcode%' => $country->zip_code_format],
                    'Shop.Forms.Errors'
               ));
                $is_valid = false;
            }
        }

        if (($hookReturn = Hook::exec('actionValidateCustomerAddressForm', ['form' => $this])) !== '') {
            $is_valid &= (bool) $hookReturn;
        }

        return $is_valid && parent::validate();
    }

 

after:

* change $controlledCountries, to the list of id countries where you want to check if there is a number in the address

public function validate()
    {
        $is_valid = true;

        $postcode = $this->getField('postcode');
        if ($postcode && $postcode->isRequired()) {
            $country = $this->formatter->getCountry();
            if (!$country->checkZipCode($postcode->getValue())) {
                $postcode->addError($this->translator->trans(
                    'Invalid postcode - should look like "%zipcode%"',
                    ['%zipcode%' => $country->zip_code_format],
                    'Shop.Forms.Errors'
               ));
                $is_valid = false;
            }
        }

        $controlledCountries = array(58, 59, 60, 61, 16, 21); // (Brazil, Brunei, Burkina Faso, Myanmar, Czechia, United States)
        $getSelectedCountry = $this->getField('id_country')->getValue();
        $address1 = $this->getField('address1');
        if ($address1) {
            $int_var = (int)filter_var($address1->getValue(), FILTER_SANITIZE_NUMBER_INT);
            if (!$int_var && (in_array($getSelectedCountry, $controlledCountries))) {
                $address1->addError($this->translator->trans(
                    'Invalid address - missing house number',
                    [],
                    'Shop.Forms.Errors'
               ));
                $is_valid = false;
            }
        }

        if (($hookReturn = Hook::exec('actionValidateCustomerAddressForm', ['form' => $this])) !== '') {
            $is_valid &= (bool) $hookReturn;
        }

        return $is_valid && parent::validate();
    }

 

Result:

obrazek.png.af89bceaac7deeb130bf5c4d13fa3f31.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...