Jump to content

I want to add 2 new text fields into the customer edit form(Backoffice)


Roshan Bhavsar

Recommended Posts


I want to add 2 new text fields below pasword field into the backoffice of the prestashop 1.7.8.7
Please guide me to add the text fields in 
admin -> customers -> select Customer -> add 2 new fields in customer edit form.
also the data should be saed into the database.

Please look for the attached image.

admin_text_field.png

Link to comment
Share on other sites

Thank you for answer.


This section of the forum is intended primarily for developers.
If you need a customized module, you can visit the JOB section and a programmer will create the module for you.

https://www.prestashop.com/forums/?forumId=235

Estimate The module development estimate is from 30 minutes to a maximum of one hour.

As a beginner programmer, you have the opportunity to study the documentation for developers, including a sample module.

https://devdocs.prestashop-project.org/1.7/modules/

Sorry, but no one here is going to write a detailed step by step procedure when everything is already written and documented.

Link to comment
Share on other sites

Hi.

Ok, you need to put your code from the module here.
I don't know the name of the field you created, I don't know the name of the column in the database table, etc.
Then I'll give you the working code.

There are also hooks to load and save data from the form.

hookActionAfterUpdateCustomerFormHandler

hookActionAfterCreateCustomerFormHandler

and custom function

Edited by 4you.software (see edit history)
Link to comment
Share on other sites

sample:

public function hookActionCustomerFormBuilderModifier(array $params)
{
    
    /* your exists code */

    $customerId = $params['id'];

    $params['data']['my_field_name_1'] = $this->getCustomerCustomFieldsData($customerId)[0];

    $params['data']['my_field_name_2'] = $this->getCustomerCustomFieldsData($customerId)[1];

    $formBuilder->setData($params['data']);
}


public function hookActionAfterUpdateCustomerFormHandler(array $params)
{
    $this->updateCustomerCustomFields($params);
}

public function hookActionAfterCreateCustomerFormHandler(array $params)
{
    $this->getCustomerCustomFieldsData($params);
}

private function getCustomerCustomFieldsData(array $params)
{
    $customerId = $params['id'];

    if (!$customerId){return;}
    
    $customerFormData = $params['form_data'];

    $getSavedData = Db::getInstance()->getRow('SELECT my_field1, my_field2 FROM '._DB_PREFIX_.'customer WHERE id_customer = '.$customerId);

    $myTextField1 = $getSavedData['my_field1'];

    $myTextField2 = $getSavedData['my_field2'];
    
    return array($myTextField1, $myTextField2);
}

private function updateCustomerCustomFields(array $params)
{
    $customerId = $params['id'];

    if (!$customerId){return;}
    
    $customerFormData = $params['form_data'];

    $myTextField1 = $customerFormData['my_field_name_1'];

    $myTextField2 = $customerFormData['my_field_name_2'];
    
    Db::getInstance()->execute("UPDATE "._DB_PREFIX_."customer SET my_field1 = '".$myTextField1."', my_field2 = '".$myTextField2."' WHERE id_customer = ".$customerId);
}

 

Link to comment
Share on other sites

On 9/21/2022 at 9:03 PM, 4you.software said:

sample:

public function hookActionCustomerFormBuilderModifier(array $params)
{
    
    /* your exists code */

    $customerId = $params['id'];

    $params['data']['my_field_name_1'] = $this->getCustomerCustomFieldsData($customerId)[0];

    $params['data']['my_field_name_2'] = $this->getCustomerCustomFieldsData($customerId)[1];

    $formBuilder->setData($params['data']);
}


public function hookActionAfterUpdateCustomerFormHandler(array $params)
{
    $this->updateCustomerCustomFields($params);
}

public function hookActionAfterCreateCustomerFormHandler(array $params)
{
    $this->getCustomerCustomFieldsData($params);
}

private function getCustomerCustomFieldsData(array $params)
{
    $customerId = $params['id'];

    if (!$customerId){return;}
    
    $customerFormData = $params['form_data'];

    $getSavedData = Db::getInstance()->getRow('SELECT my_field1, my_field2 FROM '._DB_PREFIX_.'customer WHERE id_customer = '.$customerId);

    $myTextField1 = $getSavedData['my_field1'];

    $myTextField2 = $getSavedData['my_field2'];
    
    return array($myTextField1, $myTextField2);
}

private function updateCustomerCustomFields(array $params)
{
    $customerId = $params['id'];

    if (!$customerId){return;}
    
    $customerFormData = $params['form_data'];

    $myTextField1 = $customerFormData['my_field_name_1'];

    $myTextField2 = $customerFormData['my_field_name_2'];
    
    Db::getInstance()->execute("UPDATE "._DB_PREFIX_."customer SET my_field1 = '".$myTextField1."', my_field2 = '".$myTextField2."' WHERE id_customer = ".$customerId);
}
 

 

Thanks a Lot.

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...