douju8113
2019-02-01 09:21
浏览 46
已采纳

在注册表单Prestashop 1.6.x中显示自定义字段

I added a module in Ps 1.6. which creates Electronic Invoice. This module adds in the registration form two fields but I'm not able to fetch and update data from the db.

Here below some snippets. First I create the 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);

Then I try to render data in my registration form by means of the functions below:

public function hookDisplayCustomerAccountForm($params){
$query = 'SELECT codice_destinatario FROM '._DB_PREFIX_.'customer WHERE id_customer=\'' . $params['id_customer']. '\';';


$this->context->smarty->assign('codice_destinatario', '');
$this->context->smarty->assign('pec', '');
return $this->display(__FILE__, 'hookDisplayCustomerAccountForm.tpl');
} 

public function hookDisplayCustomerIdentityForm($params){
$query = 'SELECT codice_destinatario FROM '._DB_PREFIX_.'customer WHERE id_customer=\'' . $order_id . '\';';

$this->trace("id_customer",$params['id_customer']);

$query = 'SELECT codice_destinatario FROM >'._DB_PREFIX_.'customer WHERE id_customer=\'' . $params['id_customer']. '\';';


$this->context->smarty->assign('codice_destinatario', '');
$this->context->smarty->assign('pec', '');

return $this->display(__FILE__, 'hookDisplayCustomerAccountForm.tpl');
} 

Actually the data is saved in the correct table but neither displayed in front-end nor updated.

I think I need to change also my hookDisplayCustomerAccountForm.tpl file. I tried to do this:

<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="{if isset($smarty.post.codice_destinatario)}{$smarty.post.codice_destinatario|escape:'htmlall':'UTF-8'}{/if}"/>
</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="{if isset($smarty.post.pec)}{$smarty.post.pec|escape:'htmlall':'UTF-8'}{/if}"/>
</div>

It doesn't work. Any suggestions? Thanks a lot David

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dssjxvbv918586 2019-05-14 12:08
    已采纳

    At last I found a solution. First I install a new table by this code:

    $sql = array();
        $sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'fattura24` (
            `id_fattura24` int(11) NOT NULL AUTO_INCREMENT,
            `id_customer` int(11),
            `fattura24_codice_destinatario` varchar(13),
            `fattura24_pec` varchar(60),
            PRIMARY KEY  (`id_fattura24`)
        ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;';
    
        foreach ($sql as $query) {
            if (Db::getInstance()->execute($query) == false) {
        return false;
            }
        }
    

    Then I create a hook and a template for each action in which I want to display the fields. In the example you'll see the DisplayCustomerIdentityForm:

    public function hookDisplayCustomerIdentityForm($params){
    
        $sdi_code = Tools::getValue('fattura24_codice_destinatario');
        if (!$sdi_code or strlen($sdi_code) <= 1)
        {
            $sdi_code = '0000000';    
        }
    
        if (Tools::getValue('id_fattura24')>0)
        {
        $sql = "update `"._DB_PREFIX_."fattura24` set fattura24_codice_destinatario = '".pSQL($sdi_code)."',fattura24_pec='".pSQL(Tools::getValue('fattura24_pec'))."'
        where id_fattura24 = ".(int)Tools::getValue('id_fattura24')." "; 
                   Db::getInstance()->Execute($sql);
        } elseif (Tools::getValue('fattura24_id_customer')) {
        $sql = "INSERT INTO `"._DB_PREFIX_."fattura24`(id_customer,fattura24_codice_destinatario,fattura24_pec) 
                   VALUES (".pSQL(Tools::getValue('fattura24_id_customer')).",'".pSQL($sdi_code)."','".pSQL(Tools::getValue('fattura24_pec'))."')";
                   Db::getInstance()->Execute($sql);
        }
        if ($params['cookie']->id_customer)
        {
            $sql = "select id_fattura24,fattura24_codice_destinatario,fattura24_pec from `"._DB_PREFIX_."fattura24` where id_customer = ".$params['cookie']->id_customer."";
            $result = Db::getInstance()->getRow($sql);
        }
        $this->context->smarty->assign('id_fattura24', $result['id_fattura24']);
        $this->context->smarty->assign('fattura24_id_customer', $params['cookie']->id_customer);
        $this->context->smarty->assign('fattura24_codice_destinatario', $result['fattura24_codice_destinatario']);
        $this->context->smarty->assign('fattura24_pec', $result['fattura24_pec']);
        return $this->display(__FILE__, 'views/templates/hook/customer_reg_form.tpl');
    }    
    

    Remember you have to register the hook:

     if (!parent::install()
        . . .
    
     || !$this->registerHook('displayCustomerIdentityForm')
        . . .
    
     return false;
    

    Eventually, let's show customer_reg_form.tpl:

    <input type="hidden" name="id_fattura24" value="{if isset($id_fattura24)}{$id_fattura24}{else}{/if}">
    <input type="hidden" name="fattura24_id_customer" value="{$fattura24_id_customer}">
    <div class="form-group row">
        <label>{l s='Indirizzo PEC' mod='fattura24'}</label>
        <div>
            <input type="email" name="fattura24_pec" value="{if isset($smarty.post.fattura24_pec)}{$smarty.post.fattura24_pec}{else}{$fattura24_pec}{/if}"/>
        </div>
    </div>
    <div class="form-group row">
    
        <label>{l s='Codice Destinatario' mod='fattura24'}</label>
        <div>
            <input type="text" maxlength="7" name="fattura24_codice_destinatario" value="{if isset($smarty.post.fattura24_codice_destinatario)}{$smarty.post.fattura24_codice_destinatario}{else}{$fattura24_codice_destinatario}{/if}"/>
        </div>
    </div>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题