php 在前台和后台Prestashop中显示另一个字段

64jmpszr  于 2022-12-17  发布在  PHP
关注(0)|答案(1)|浏览(95)

我想显示一个“功能”字段在前台和后台的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);
    }

有人能帮我吗:)?

qij5mzcb

qij5mzcb1#

您需要注册这些新的额外钩子
1.验证客户表单字段
1.操作对象客户更新时间
1.操作对象客户添加时间
1.创建客户表单处理程序后的操作

  1. actionAfterUpdateCustomerFormHandler,就像您在上面的问题中所做的那样
    $this->registerHook('validateCustomerFormFields');
  2. validateCustomerFormFields用于验证表单字段。
public function hookValidateCustomerFormFields($params)
{
      $myfields = $params['fields'];
      // example check if fonction is empty
       if($myfields[0]->getValue() ==''){
          $myfields[0]->addError(
              $this->l('fonction is empty')
          );
       }
      return array($myfields);

}
  1. actionObjectCustomerUpdateAfter当客户更新他/她的身份表单时调用此挂接
public function actionObjectCustomerUpdateAfter($params)
    {
            $id_customer = (int)$params['object']->id;
           $fonction= trim(Tools::getValue('fonction'));
             Db::getInstance()->update('customer', array(fonction=>pSQL($fonction) ), ' id_customer ='.(int) $id_customer);
    
           
    }

1.操作对象客户添加时间
客户创建帐户时调用此挂接。

public function actionObjectCustomerAddAfter($params)
    {
        $id_customer = (int)$params['object']->id;
        $fonction= trim(Tools::getValue('fonction'));
        Db::getInstance()->update('customer', array(fonction=>pSQL($fonction) ), ' id_customer ='.(int) $id_customer);
    }
  1. actionAfterCreateCustomerFormHandler创建客户帐户时调用此挂接
public function hookActionAfterCreateCustomerFormHandler($params)
    {
       $id_customer =  $params['id']; 
       $formcontent =  $params['form_data']; // this is an array of form fields and values.
       // You can update your the fonction field this way
      Db::getInstance()->update('customer', array(fonction=>$formcontent['fonction']), ' id_customer ='.(int) $id_customer);
    
              
    }
  1. actionAfterUpdateCustomerFormHandler在更新客户帐户时调用此挂接。
public function hookActionAfterUpdateCustomerFormHandler($params)
    {
    $id_customer =  $params['id']; 
    $formcontent =  $params['form_data']; // this is an array of form fields and values.
    You can update your the fonction field this way
    Db::getInstance()->update('customer', array(fonction=>$formcontent['fonction']), ' id_customer ='.(int) $id_customer);     
    }

如果您要做的只是向customer表单中添加附加字段,则不需要包含这些挂接
1.操作客户帐户添加
1.操作客户帐户更新
1.显示客户帐户

相关问题