Jump to content

Custom Fields Registration Form


Recommended Posts

Hi,
I've created a custom module to add fields to registration form but something isn't working
I've tried to change position of fields using this tutorial https://webkul.com/blog/adding-new-field-in-prestashop-1-7-admin-symfony-controller/ but remain same positions as before. Why?
Fields are saved but tipo_anagrafica and pec doesn't show up. Why?

This is my code. Prestashop version 1.7.7.7

protected function alterCustomerTable()
    {
        $sql = array();
        $sql[] = 'ALTER TABLE `' . pSQL(_DB_PREFIX_) . 'customer` ADD `tipo_anagrafica` VARCHAR(2) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL';
        $sql[] = 'ALTER TABLE `' . pSQL(_DB_PREFIX_) . 'customer` ADD `pec` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL';
        $sql[] = 'ALTER TABLE `' . pSQL(_DB_PREFIX_) . 'customer` ADD `sdi` VARCHAR(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL';
        $sql[] = 'ALTER TABLE `' . pSQL(_DB_PREFIX_) . 'customer` ADD `codice_fiscale` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL';
        foreach($sql as $s){
            if(!Db::getInstance()->execute($s))
                return false;
        }

        return true;
    }
 public function hookAdditionalCustomerFormFields($params)
    {
        return [
            (new FormField)
                ->setName('tipo_anagrafica')
                ->setType('select')
                ->setAvailableValues(['PR' => 'Persona Fisica', 'BU' => 'Società '])
                ->setRequired(true)
                ->setLabel($this->l('Tipo Anagrafica')),
            (new FormField)
                ->setName('codice_fiscale')
                ->setType('text')
                ->setLabel($this->l('Codice Fiscale')),
            (new FormField)
                ->setName('pec')
                ->setType('text')
                ->setRequired(false) 
                ->setLabel($this->l('pec')),
            (new FormField)
                ->setName('sdi')
                ->setType('text')
                ->setRequired(false)
                ->setLabel($this->l('sdi')),
        ];
    }


    /**
     * Hook allows to modify Customers form and add additional form fields as well as modify or add new data to the forms.
     * BACK_END
     * @param array $params
     */
    public function hookActionCustomerFormBuilderModifier(array $params)
    {
        /** @var FormBuilderInterface $formBuilder \Symfony\Component\Form */
        $formBuilder = $params['form_builder'];
        $allFields = $formBuilder->all();

        foreach ($allFields as $inputField => $input) {
            $formBuilder->remove($inputField);
        }

        $count = 0;
        foreach ($allFields as $inputField => $input) {
            
            if($count == 0){
                /** First Field */
                $formBuilder->add('tipo_anagrafica', ChoiceType::class, [
                    'choices'  => [
                        'Persona Fisica' => 'PR',
                        'Società' => 'BU',
                    ],
                    'label' => $this->getTranslator()->trans('TIPO ANAGRAFICA', [], 'Modules.ps_customercedula.Admin'),
                    'required' => false,
                ]);

                /** Second Field */
                $formBuilder->add('codice_fiscale', TextType::class, [
                    'label' => $this->getTranslator()->trans('CODICE FISCALE', [], 'Modules.ps_customercedula.Admin'),
                    'required' => false,
                ]);        

                /** Third Field */
                $formBuilder->add('pec', TextType::class, [
                    'label' => $this->getTranslator()->trans('PEC', [], 'Modules.ps_customercedula.Admin'),
                    'required' => false,
                ]);

                /** Fourth Field */
                $formBuilder->add('sdi', TextType::class, [
                    'label' => $this->getTranslator()->trans('SDI', [], 'Modules.ps_customercedula.Admin'),
                    'required' => false,
                ]);

                $count++;
            }
            $formBuilder->add($input);
        }
        $customer = new Customer($params['id']);

        $params['data']['tipo_anagrafica'] = $customer->tipo_anagrafica;
        $params['data']['codice_fiscale'] = $customer->codice_fiscale;        
        $params['data']['sdi'] = $customer->sdi;
        $params['data']['pec'] = $customer->pec;

        $formBuilder->setData($params['data']);
    }


    /**
     * Hook allows to modify Customers form and add additional form fields as well as modify or add new data to the forms.
     *
     * @param array $params
     *
     * @throws CustomerException
     */
    public function hookActionAfterUpdateCustomerFormHandler(array $params)
    {
        $this->updateCustomerCedula($params);
    }

    /**
     * Hook allows to modify Customers form and add additional form fields as well as modify or add new data to the forms.
     *
     * @param array $params
     *
     * @throws CustomerException
     */
    public function hookActionAfterCreateCustomerFormHandler(array $params)
    {
        $this->updateCustomerCedula($params);
    }

    /**
     * Update / Create 
     * 
     * @param array $params
     *
     * @throws \PrestaShop\PrestaShop\Core\Module\Exception\ModuleErrorException
     */
    private function updateCustomerCedula(array $params)
    {
        $customerId = (int)$params['id'];
        /** @var array $customerFormData */
        $customerFormData = $params['form_data'];

        $tipo_anagrafica = $customerFormData['tipo_anagrafica'];
        $codice_fiscale = $customerFormData['codice_fiscale'];
        $sdi = $customerFormData['sdi'];
        $pec = $customerFormData['pec'];
        try {

            $customer = new Customer($customerId);
            $customer->tipo_anagrafica= $tipo_anagrafica;
            $customer->codice_fiscale= $codice_fiscale;
            $customer->pec= $pec;
            $customer->sdi= $sdi;

            $customer->update();

        } catch (ReviewerException $exception) {
            throw new \PrestaShop\PrestaShop\Core\Module\Exception\ModuleErrorException($exception);
        }
    }



 

test1.JPG

test2.JPG

Identity.png

  • Like 1
Link to comment
Share on other sites

  • 9 months later...

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