I want to make a module in prestashop 8.1 that creates a new field at the time of registration and at the time of payment without login send this information to the database in a column in the Customers table and finally show the value that is in this column in the customer information in the backoffice
i try to make it by some tutorials and reading the documentation but its doesnt work (sorry for bad english)
v1:
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class Cpf extends Module{
public function __construct() {
$this->name = 'cpf';
$this->tab = 'front_office_features';
$this->version = '1.0.4.2';
$this->author = 'Henrique Barbosa Sampaio';
$this->need_instance = 0;
$this->ps_versions_compliancy = [
'min' => '1.7.0.0',
'max' => '8.99.99',
];
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('cpf');
$this->description = $this->l('Descrição do meu módulo.');
$this->confirmUninstall = $this->l('Tem certeza de que deseja desinstalar?');
}
public function install(){
if(!parent::install() ||
!$this->registerHook('leftColumn') ||
!$this->registerHook('header') ||
!$this->registerHook('additionalCustomerFormFields')||
!Configuration::updateValue('cpf', 'my friend')){
return false;
}
return true;
}
public function uninstall(){
if (!parent::uninstall() ||
!Configuration::deleteByName('cpf')
) {
return false;
}
return true;
}
public function hookAdditionalCustomerFormFields($params){
$extra_fields = array();
$extra_fields['cpf'] = (new FormField)
->setName('cpf')
->setType('text')
->setLabel($this->l('CPF'))
->setRequired(true);
return $extra_fields;
}
public function hookvalidateCustomerFormFields($params){
echo "method called teste brasileiro";
$module_fields = $params['fields'];
$cpf = $module_fields['cpf']->getValue();
error_log('CPF value: '.$cpf);
// Adicione aqui a validação para o campo CPF
if (empty($cpf)) {
$module_fields['cpf']->addError($this->l('O campo CPF é obrigatório.'));
} elseif (!preg_match('/^\d{3}\.\d{3}\.\d{3}-\d{2}$/', $cpf)) {
$module_fields['cpf']->addError($this->l('O formato do CPF é inválido.'));
}else{
$id_customer = (int)$params['newCustomer']->id;
$sql = 'UPDATE `'._DB_PREFIX_.'customer` SET `cpf` = \''.pSQL($cpf).'\' WHERE `id_customer` = '.$id_customer;
error_log('SQL query: '.$sql);
Db::getInstance()->execute($sql);
}
return $module_fields;
}
}
?>
v2 using the documentation:
<?php
if(!defined('_PS_VERSION_')){
exit;
}
class Cpf extends Module{
public function __construct(){
$this->name = 'Cpf module';
$this->tab = 'front_office_features';
$this->version = '1.4.0';
$this->author = 'Henrique Barbosa Sampaio';
$this->need_instance = 1;
$this->ps_versions_compliancy = [
'min' => '1.7.0.0',
'max' => '8.99.99',
];
$this->bootstrap = true;
$this->bootstrap = true;
$this->displayName = $this->l('Modulo cpf');
$this->description = $this->l('modulo para adicionar o campo cpf no prestashop futuramente tera um check de cpf e uma estruturação mais bem feita.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
if (!Configuration::get('Cpf')) {
$this->warning = $this->l('Se nome provido');
}
}
public function install()
{
if (Shop::isFeatureActive()) {
Shop::setContext(Shop::CONTEXT_ALL);
}
return parent::install() &&
$this->registerHook('displayLeftColumn') &&
$this->registerHook('actionFrontControllerSetMedia') &&
$this->registerHook('displayRightColumn') &&
Configuration::updateValue('Cpf', 'my module');
}
}
?>