Jump to content

custom fields in registration form prestashop 1.6


Recommended Posts

I'm updating a module for Prestashop 1.6,

and I need to add two text fields to Customer registration form, but I didn't succeed in showing and updating data. 

here below some code from my module:

class Fattura24 extends Module{
 [...]
    public function install(){
[...]
//manually add new field in customer table
        $sqlInstallCod = 'ALTER TABLE ' ._DB_PREFIX_.'customer ADD codice_destinatario VARCHAR(255) DEFAULT NULL';
        Db::getInstance()->execute($sqlInstallCod);
        $sqlInstallPec = 'ALTER TABLE ' ._DB_PREFIX_.'customer ADD pec VARCHAR(255) DEFAULT NULL';
        Db::getInstance()->execute($sqlInstallPec);
[...]
// register hooks   
        if (!parent::install() 
            || !$this->registerHook('displayCustomerAccountForm')
            || !$this->registerHook('displayCustomerIdentityForm')
            || !$this->registerHook('actionCustomerAccountAdd')
            || !$this->registerHook('actionValidateOrder')
            || !$this->registerHook('actionOrderStatusUpdate')
            || !$this->registerHook('actionOrderStatusPostUpdate'))
            return false;
[...]
        return true;
}

here below the hooks: 

public function hookDisplayCustomerAccountForm($params){
        
        $query = 'SELECT codice_destinatario,pec FROM '._DB_PREFIX_.'customer WHERE id_customer=\''.$params['id_customer'] . '\';';
        $results = Db::getInstance()->getRow($query);
        
        if ( $results ) {
        $cod = $results['cod'];
        $pec = $results['pec'];
        }       
        
        $this->context->smarty->assign('codice_destinatario', $cod);
        $this->context->smarty->assign('pec', $pec);
        return $this->display(__FILE__, 'hookDisplayCustomerAccountForm.tpl');
    } 

public function hookDisplayCustomerAccountForm($params){
        
        $idCustomer = $params['Customer']->id;
        $query = 'SELECT codice_destinatario,pec FROM '._DB_PREFIX_.'customer WHERE id_customer=\''.$idCustomer . '\';';
        $results = Db::getInstance()->getRow($query);
        
        if ( $results ) {
        $cod = $results['cod'];
        $pec = $results['pec'];
        }       
        
        $this->context->smarty->assign('codice_destinatario', $cod);
        $this->context->smarty->assign('pec', $pec);
        return $this->display(__FILE__, 'hookDisplayCustomerAccountForm.tpl');
    } 

    public function hookDisplayCustomerIdentityForm($params){

        $idCustomer = $params['Customer']->id;
        $query = 'SELECT codice_destinatario FROM '._DB_PREFIX_.'customer WHERE id_customer=\'' . $idCustomer . /*$order_id . */'\';';
        $this->trace("hookDisplayMyAccountBlockfooter",$params);
        $this->trace("id_customer",$params['id_customer']);
        
        $codice_destinatario = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($query)['docIdOrderFattura24'];
            
        $this->trace("hookDisplayMyAccountBlockfooter",$params);
        $this->context->smarty->assign('codice_destinatario', $cod);
        $this->context->smarty->assign('pec', $pec);
        return $this->display(__FILE__, 'hookDisplayCustomerAccountForm.tpl');
    }
 
    /**
     * save customer pec and destination code in table
     */
    public function hookActionCustomerAccountAdd($params){

        $pec = Tools::getValue('pec', $pec);
        $codice_destinatario = Tools::getValue('codice_destinatario', $cod);
        $idCustomer = $params['newCustomer']->id;
        $this->trace("idCustomer", $idCustomer);
        
        $queryCodiceDestinatario = 'UPDATE ' . _DB_PREFIX_ . 'customer SET codice_destinatario=\'' . $codice_destinatario . '\' WHERE id_customer=\'' . $idCustomer . '\';';
        Db::getInstance()->Execute($queryCodiceDestinatario);
        $queryPec = 'UPDATE ' . _DB_PREFIX_ . 'customer SET pec=\'' . $pec . '\' WHERE id_customer=\'' . $idCustomer . '\';';
        Db::getInstance()->Execute($queryPec);
    }

Actually I'm able to save data when customer is at checkout, but I cannot display nothing when editing or updating data in My account. 

Here below the template:

<div class="form-group">
    <label for="codice_destinatario">{l s='Codice Destinatario' mod='fattura24'}</label>
    <input class="form-control" name="codice_destinatario" type="text" id="codice_destinatario" value={codice_destinatario}/>
</div>
<div class="form-group">
    <label for="pec">{l s='PEC' mod='fattura24'}</label>
    <input class="form-control" name="pec" type="text" id="pec" value={pec}/>
</div>

I see the input field, but I cannot neither see nor update its content.

Any idea to solve?

Thanks,

David

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