我想显示一个“功能”字段在前台和后台的prestashop,但我不能。由于我的代码,我添加了字段“功能”在我的注册表中,它是持久化在数据库中,我们可以修改它也在数据库中,但是前台和后台都不显示,它的值没有填入form-field.tpl的$value变量中,我也不I don’我不知道怎样修理它
我的模块:
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
use PrestaShop\PrestaShop\Adapter\Presenter\Object\ObjectPresenter;
require_once dirname(__FILE__) . '/classes/CustomerFonction.php';
class AddJob extends Module
{
public function __construct()
{
$this->name = 'addjob';
$this->tab = 'others';
$this->version = '1.0.0';
$this->author = 'advisa';
$this->need_instance = 0;
$this->ps_versions_compliancy = [
'min' => '1.6',
'max' => '1.7.99',
];
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('AddJob');
$this->description = $this->l('Add job in registration form');
}
public function install()
{
return parent::install() &&
$this->installSql() &&
$this->registerHook('additionalCustomerFormFields') &&
$this->registerHook('actionCustomerAccountAdd') &&
$this->registerHook('actionCustomerAccountUpdate') &&
$this->registerHook('actionAdminCustomersFormModifier') &&
$this->registerHook('displayCustomerAccount');
}
public function uninstall()
{
return (
parent::uninstall()
);
}
/*
public function installSql()
{
$sqlInstall = "ALTER TABLE " . _DB_PREFIX_ . "customer "
. "ADD fonction VARCHAR(255) NULL";
return Db::getInstance()->execute($sqlInstall);
} */
public function installSql()
{
$sqlQuery = array();
$sqlQuery[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'addjob_addfonction` (
`id_addfonction` INT(10) PRIMARY KEY NOT NULL AUTO_INCREMENT,
`id_customer` INT(10),
`fonction` VARCHAR(255)
)';
$db = Db::getInstance();
foreach ($sqlQuery as $query) {
if (!$db->execute($query)) {
return false;
}
}
return true;
}
public function hookAdditionalCustomerFormFields($param)
{
$field = array();
$field['fonction'] = (new FormField())
->setName('fonction')
->setType('text')
//->setRequired(true)
->setLabel($this->l('Fonction'));
if($this->context->customer->id !== null){
$data = new CustomerFonction($this->context->customer->id);
$field['fonction']->setValue($data->fonction ?? '');
}
return $field;
}
public function hookActionCustomerAccountAdd($param)
{
$customeField = new CustomerFonction();
$customeField->id_customer = $param['newCustomer']->id;
$customeField->fonction = Tools::getValue('fonction', '');
$customeField->add();
}
public function hookActionCustomerAccountUpdate($param)
{
$customerId = $this->context->customer->id;
$customerField = new CustomerFonction($customerId);
$customerField->id_customer = $customerId;
$customerField->fonction = Tools::getValue('fonction', '');
$customerField->save();
}
public function hookActionAdminCustomersFormModifier($params) {
$params['fields'][0]['form']['input'][] = [
'type' => 'text',
'label' => $this->l('fonction'),
'name' => 'fonction',
'class' => 'input fixed-width-xxl',
'hint' => $this->l('fonction')
];
//Définition de la valeur du champ supplémentaire
$params['fields_value']['fonction'] = $params['object']->fonction;
}
public function displayCustomerAccount($param)
{
$this->context->smarty->assign([
'fonction' => Tools::getValue('fonction')
]);
return '/themes/classic/templates/_partials/form-field.tpl';
}
}
这是我的类扩展ObjectModel
<?php
class CustomerFonction extends ObjectModel
{
public $id_addfonction;
public $id_customer;
public $fonction;
public static $definition = [
'table' => 'addjob_addfonction',
'primary' => 'id_addfonction',
'multilang' => false,
'fields' => [
'id_customer' => ['type' => self::TYPE_INT],
'fonction' => ['type'=>self::TYPE_STRING, 'size'=> 255],
],
];
public static function getFonctionByCustomer($id_customer): ?string
{
$sql = new DbQuery();
$sql->select('fonction');
$sql->from('addjob_fonction');
$sql->where('id_customer = ' . $id_customer);
return Db::getInstance()->getValue($sql);
}
有人能帮我吗:)?
1条答案
按热度按时间qij5mzcb1#
您需要注册这些新的额外钩子
1.验证客户表单字段
1.操作对象客户更新时间
1.操作对象客户添加时间
1.创建客户表单处理程序后的操作
$this->registerHook('validateCustomerFormFields');
1.操作对象客户添加时间
客户创建帐户时调用此挂接。
如果您要做的只是向customer表单中添加附加字段,则不需要包含这些挂接
1.操作客户帐户添加
1.操作客户帐户更新
1.显示客户帐户