Php

PrestaShop Custom Registration Field

Hello friend I am going to teach you in this tutorial that if you want to add a new field in the registration form of PrestaShop,1.7 how can you do it .

So for this I will add the phone number to my PrestaShop 1.7 registration form. Which will be required for the customer. But at the same time I will also tell that if the field is not to be required then how will you do it.

Before starting the further process, see these two images below. Here the 1st image has the default PrestaShop 1.7 registration and the 2nd image has a new field with the phone number added after.

Video of this tutorial is also available: CLICK HERE

Let’s start with adding new columns called phone to the ps_customer table . So go to your phpMyAdmin and execute the following SQL command.

ALTER TABLE ps_customer ADD COLUMN phone VARCHAR(250);

Now we have to edit in some files:

  1. classes/form/CustomerFormatter.php
  2. classes/Customer.php
  1. Edit : CustomerFormatter.php

Now it’s time to edit CustomerFormatter class to make our fields visible in the registration form.

You have to find the getFormat() function .

OR You can also find this line

$genders = Gender::getGenders($this->language->id);

Now in this function you have to add this couple of code look like this –

Code :

// New Field added Here --------------------------------------->
    
           $format['phone'] = (new FormField)
            ->setName('phone')
            ->setLabel(
                $this->translator->trans(
                    'Phone', [], 'Shop.Forms.Labels'
                )
            )
            ->setRequired(true);


//End Of here ------------------------------------------------------>

If you don’t want to keep this field Required then you can remove this line

 ->setRequired(true)

You can change the position of new fields(Phone) and put it wherever you want.

2. Edit : Customer.php

Now Customer model by adding new columns. You can also add there a validation and determine if the field must be required.

In this file you have to add this line look like this :

Code :

    //New field Added here ------------------->

         public $phone;

    //End Here 

Now Go to the public static $definition and here add this line like :

Code :

        //New Field added Here ------------------>
             'phone' => array('type' => self::TYPE_INT, 'required' => true),
        //End here ------------------------------>

Here is the phone number, so TYPE_INT is kept and if there is some other field, then you can keep it string, bool, you can see it in the other field

RECOMMENDED ARTICLES





Leave a Reply

Your email address will not be published. Required fields are marked *