Reputation: 11
Hi I am relatively new to prestashop and wondering if someone could direct me towards a right place.
Basically, I am after adding a new custom field to the create_account page.
I was initially hack fixing the issue, but I know that once prestashop is updated all of the changes to core files will be overwritten.
I have began to do my changes to the AdminCustomerController.php as below.(Also the field I am trying to add is the Nature of Business)
class AdminCustomersController extends AdminCustomersControllerCore {
public function renderForm($id = null)
{
$this->fields_form = array(
'legend' => array(
'title' => $this->trans('Customer', array(), 'Admin.Global'),
'icon' => 'icon-user'
),
'input' => array(
array(
'type' => 'text',
'prefix' => '',
'label' => $this->trans('Practice', array(), 'Admin.Global'),
'name' => 'practice',
'col' => '4',
'autocomplete' => false
),
array(
'type' => 'text',
'label' => $this->trans('Nature of Business', array(), 'Admin.Global'),
'name' => 'nature_enquiry',
'col' => '4',
),
)
);
return parent::renderForm();
}
}
Also edited the Customer.php in override folder. as below:
class Customer extends CustomerCore {
public $practice_from;
/** nature of enquiry */
public $nature_enquiry;
protected $definition = array(
'table' => 'customer',
'primary' => 'id_customer',
'fields' => array(
'nature_enquiry' => array('type' => self::TYPE_HTML, 'validate' => 'isCleanHtml', 'required'=>false,'size' => 65000),
),
);
public function __construct($id = null)
{
parent::__construct($id);
parent::$definition['practice_from'] = ['type' => parent::TYPE_STRING];
}
}
lastly added my new field to the classes/Customer.php at the bottom of the definition array as below:
public static $definition = array(
'table' => 'customer',
'primary' => 'id_customer',
'fields' => array(
'secure_key' => array('type' => self::TYPE_STRING, 'validate' => 'isMd5', 'copy_post' => false),
'lastname' => array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 255),
'firstname' => array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 255),
'email' => array('type' => self::TYPE_STRING, 'validate' => 'isEmail', 'required' => true, 'size' => 128),
'passwd' => array('type' => self::TYPE_STRING, 'validate' => 'isPasswd', 'required' => true, 'size' => 60),
'last_passwd_gen' => array('type' => self::TYPE_STRING, 'copy_post' => false),
'id_gender' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'birthday' => array('type' => self::TYPE_DATE, 'validate' => 'isBirthDate'),
'newsletter' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'newsletter_date_add' => array('type' => self::TYPE_DATE, 'copy_post' => false),
'ip_registration_newsletter' => array('type' => self::TYPE_STRING, 'copy_post' => false),
'optin' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'website' => array('type' => self::TYPE_STRING, 'validate' => 'isUrl'),
'company' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
'siret' => array('type' => self::TYPE_STRING, 'validate' => 'isSiret'),
'ape' => array('type' => self::TYPE_STRING, 'validate' => 'isApe'),
'outstanding_allow_amount' => array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat', 'copy_post' => false),
'show_public_prices' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
'id_risk' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'copy_post' => false),
'max_payment_days' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'copy_post' => false),
'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
'deleted' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
'note' => array('type' => self::TYPE_HTML, 'validate' => 'isCleanHtml', 'size' => 65000, 'copy_post' => false),
'is_guest' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
'id_shop' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'copy_post' => false),
'id_shop_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'copy_post' => false),
'id_default_group' => array('type' => self::TYPE_INT, 'copy_post' => false),
'id_lang' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'copy_post' => false),
'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false),
'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate', 'copy_post' => false),
'reset_password_token' => array('type' => self::TYPE_STRING, 'validate' => 'isSha1', 'size' => 40, 'copy_post' => false),
'reset_password_validity' => array('type' => self::TYPE_DATE, 'validate' => 'isDateOrNull', 'copy_post' => false),
'nature_enquiry' => array('type' => self::TYPE_HTML, 'validate' => 'isCleanHtml', 'required'=>false,'size' => 65000),
),
);
classes/form/CustomerFormatter.php
$format['nature_enquiry'] = (new FormField)
->setName('nature_enquiry')
->setLabel(
$this->translator->trans(
'Nature of Business', [], 'Shop.Forms.Labels'
)
);
Ideally I wanted to know I have missed anything in order to get this working on the live site.
or any documentation for adding a custom field to the registrations form.
PrestaShop - 1.7.0.6
Thanks
Upvotes: 1
Views: 3727
Reputation: 169
You must avoid editing core codes. overrides is not recommended too. you should try to create a module.
You can use this hooks in your module:
public function hookAdditionalCustomerFormFields($params)
{
//return html of your field
}
public function hookActionSubmitAccountBefore($params)
{
//process your field posted data
}
public function hookActionAdminCustomersListingFieldsModifier($params)
{
//show your filed in admin customers list
$params['fields']['your_new_field'] = array(
'title' => $this->l('label'),
'align' => 'center',
);
}
public function hookActionAdminCustomersFormModifier($params)
{
dd($params);
//add your fields
}
Upvotes: 1
Reputation: 568
Haven't looked at your changes in detail, but they look solid at first glance. To keep these changes when updating you will need to "override" these files. See the official docs on overrides. It basically consists of creating the appropriate files in the [prestashop_root_dir]/overrides
directory and rewriting the methods you want to change.
You will also need to delete [prestashop_root_dir]/app/cache/pro/class_index.php
so the overriden files get loaded.
Upvotes: 0