Jump to content

How save field /example dni/ into ps_address in 1st step of registration without changing registration way


Recommended Posts

Hi,

Prestashop 1.6.1.23

I have registration set up to Only Create Account. I have added custom field to ps_address. I have amended couple of files to get it work this way. My problem is with this settings PrestaShop is not creating record in ps_address but I need to save all fields I put into my registration field into DB.
So what I want to do is evn with this settings to be allow override something to save part of address as dni, vat_number and my_custom_field in very first step. Basically new record will be creared even without saving required address etc ... I need to tell Presta to save my custom field with name ,,ic_dph,, , dni and vat_number in first step optionally even if there is no address filled in or even displayed in first registration form

I added following into my theme/address.tpl

{if $field_name eq 'ic_dph'}
                <div id="vat_area">
                    <div id="ic_dph">
                        <div class="form-group">
                            <label for="ic-dph">{l s='IC DPH'}</label>
                            <input type="text" class="form-control validate" data-validate="{$address_validation.$field_name.validate}" id="ic-dph" name="ic_dph" value="{if isset($smarty.post.ic_dph)}{$smarty.post.ic_dph}{else}{if isset($address->ic_dph)}{$address->ic_dph|escape:'html':'UTF-8'}{/if}{/if}" />
                        </div>
                    </div>
                </div>
            {/if}
 
I added following code into overrides/classes/Address.php  -> $ic_dph is my custom field -
 
<?php
 
class Address extends AddressCore
{
 
    public $ic_dph;
 
    public static $definition = array(
        'table' => 'address',
        'primary' => 'id_address',
        'fields' => array(
            'id_customer' =>        array('type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId', 'copy_post' => false),
            'id_manufacturer' =>    array('type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId', 'copy_post' => false),
            'id_supplier' =>        array('type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId', 'copy_post' => false),
            'id_warehouse' =>       array('type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId', 'copy_post' => false),
            'id_country' =>         array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
            'id_state' =>           array('type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId'),
            'alias' =>              array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 32),
            'company' =>            array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 64),
            'lastname' =>           array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 32),
            'firstname' =>          array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 32),
            'vat_number' =>         array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
            'address1' =>           array('type' => self::TYPE_STRING, 'validate' => 'isAddress', 'required' => true, 'size' => 128),
            'address2' =>           array('type' => self::TYPE_STRING, 'validate' => 'isAddress', 'size' => 128),
            'postcode' =>           array('type' => self::TYPE_STRING, 'validate' => 'isPostCode', 'size' => 12),
            'city' =>               array('type' => self::TYPE_STRING, 'validate' => 'isCityName', 'required' => true, 'size' => 64),
            'other' =>              array('type' => self::TYPE_STRING, 'validate' => 'isMessage', 'size' => 300),
            'phone' =>              array('type' => self::TYPE_STRING, 'validate' => 'isPhoneNumber', 'size' => 32),
            'phone_mobile' =>       array('type' => self::TYPE_STRING, 'validate' => 'isPhoneNumber', 'size' => 32),
            'dni' =>                array('type' => self::TYPE_STRING, 'validate' => 'isDniLite', 'size' => 16),
            'deleted' =>            array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'copy_post' => false),
            'date_add' =>           array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat', 'copy_post' => false),
            'date_upd' =>           array('type' => self::TYPE_DATE, 'validate' => 'isDateFormat', 'copy_post' => false),
            'ic_dph' =>     array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 64),
 
        ),
    );
}
 
I added following code into overrides/controllers/admin/AdminAddressesController.php
 
<?php
 
Class AdminAddressesController extends AdminAddressesControllerCore
{
 
    public function renderForm()
    {
        $this->fields_form = array(
            'legend' => array(
                'title' => $this->l('Addresses'),
                'icon' => 'icon-envelope-alt'
            ),
            'input' => array(
                array(
                    'type' => 'text_customer',
                    'label' => $this->l('Customer'),
                    'name' => 'id_customer',
                    'required' => false,
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Identification Number'),
                    'name' => 'dni',
                    'required' => false,
                    'col' => '4',
                    'hint' => $this->l('DNI / NIF / NIE')
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('IC DPH'),
                    'name' => 'ic_dph',
                    'required' => false,
                    'col' => '4',
                    'hint' => $this->l('IC DPH!')
                ),              
                array(
                    'type' => 'text',
                    'label' => $this->l('Address alias'),
                    'name' => 'alias',
                    'required' => true,
                    'col' => '4',
                    'hint' => $this->l('Invalid characters:').' &lt;&gt;;=#{}'
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Home phone'),
                    'name' => 'phone',
                    'required' => false,
                    'col' => '4',
                    'hint' => Configuration::get('PS_ONE_PHONE_AT_LEAST') ? sprintf($this->l('You must register at least one phone number.')) : ''
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Mobile phone'),
                    'name' => 'phone_mobile',
                    'required' => false,
                    'col' => '4',
                    'hint' => Configuration::get('PS_ONE_PHONE_AT_LEAST') ? sprintf($this->l('You must register at least one phone number.')) : ''
                ),
                array(
                    'type' => 'textarea',
                    'label' => $this->l('Other'),
                    'name' => 'other',
                    'required' => false,
                    'cols' => 15,
                    'rows' => 3,
                    'hint' => $this->l('Forbidden characters:').' &lt;&gt;;=#{}'
                ),
            ),
            'submit' => array(
                'title' => $this->l('Save'),
            )
        );
        $id_customer = (int)Tools::getValue('id_customer');
        if (!$id_customer && Validate::isLoadedObject($this->object))
            $id_customer = $this->object->id_customer;
        if ($id_customer)
        {
            $customer = new Customer((int)$id_customer);
            $token_customer = Tools::getAdminToken('AdminCustomers'.(int)(Tab::getIdFromClassName('AdminCustomers')).(int)$this->context->employee->id);
        }
 
        $this->tpl_form_vars = array(
            'customer' => isset($customer) ? $customer : null,
            'tokenCustomer' => isset ($token_customer) ? $token_customer : null
        );
 
        // Order address fields depending on country format
        $addresses_fields = $this->processAddressFormat();
        // we use delivery address
        $addresses_fields = $addresses_fields['dlv_all_fields'];
 
        $temp_fields = array();
 
        foreach ($addresses_fields as $addr_field_item)
        {
            if ($addr_field_item == 'company')
            {
                $temp_fields[] = array(
                    'type' => 'text',
                    'label' => $this->l('Company'),
                    'name' => 'company',
                    'required' => false,
                    'col' => '4',
                    'hint' => $this->l('Invalid characters:').' &lt;&gt;;=#{}'
                );
                $temp_fields[] = array(
                    'type' => 'text',
                    'label' => $this->l('VAT number'),
                    'col' => '2',
                    'name' => 'vat_number'
                );
            }
            else if ($addr_field_item == 'lastname')
            {
                if (isset($customer) &&
                    !Tools::isSubmit('submit'.strtoupper($this->table)) &&
                    Validate::isLoadedObject($customer) &&
                    !Validate::isLoadedObject($this->object))
                    $default_value = $customer->lastname;
                else
                    $default_value = '';
 
                $temp_fields[] = array(
                    'type' => 'text',
                    'label' => $this->l('Last Name'),
                    'name' => 'lastname',
                    'required' => true,
                    'col' => '4',
                    'hint' => $this->l('Invalid characters:').' 0-9!&amp;lt;&amp;gt;,;?=+()@#"�{}_$%:',
                    'default_value' => $default_value,
                );
            }
            else if ($addr_field_item == 'firstname')
            {
                if (isset($customer) &&
                    !Tools::isSubmit('submit'.strtoupper($this->table)) &&
                    Validate::isLoadedObject($customer) &&
                    !Validate::isLoadedObject($this->object))
                    $default_value = $customer->firstname;
               else
                   $default_value = '';
 
                $temp_fields[] = array(
                    'type' => 'text',
                    'label' => $this->l('First Name'),
                    'name' => 'firstname',
                    'required' => true,
                    'col' => '4',
                    'hint' => $this->l('Invalid characters:').' 0-9!&amp;lt;&amp;gt;,;?=+()@#"�{}_$%:',
                    'default_value' => $default_value,
                );
            }
            else if ($addr_field_item == 'address1')
            {
                $temp_fields[] = array(
                    'type' => 'text',
                    'label' => $this->l('Address'),
                    'name' => 'address1',
                    'col' => '6',
                    'required' => true,
                );
            }
            else if ($addr_field_item == 'address2')
            {
                $temp_fields[] = array(
                    'type' => 'text',
                    'label' => $this->l('Address').' (2)',
                    'name' => 'address2',
                    'col' => '6',
                    'required' => false,
                );
            }
            elseif ($addr_field_item == 'postcode')
            {
                $temp_fields[] = array(
                    'type' => 'text',
                    'label' => $this->l('Zip/Postal Code'),
                    'name' => 'postcode',
                    'col' => '2',
                    'required' => true,
                );
            }
            else if ($addr_field_item == 'city')
            {
                $temp_fields[] = array(
                    'type' => 'text',
                    'label' => $this->l('City'),
                    'name' => 'city',
                    'col' => '4',
                    'required' => true,
                );
            }
            else if ($addr_field_item == 'country' || $addr_field_item == 'Country:name')
            {
                $temp_fields[] = array(
                    'type' => 'select',
                    'label' => $this->l('Country'),
                    'name' => 'id_country',
                    'required' => false,
                    'col' => '4',
                    'default_value' => (int)$this->context->country->id,
                    'options' => array(
                        'query' => Country::getCountries($this->context->language->id),
                        'id' => 'id_country',
                        'name' => 'name'
                    )
                );
                $temp_fields[] = array(
                    'type' => 'select',
                    'label' => $this->l('State'),
                    'name' => 'id_state',
                    'required' => false,
                    'col' => '4',
                    'options' => array(
                        'query' => array(),
                        'id' => 'id_state',
                        'name' => 'name'
                    )
                );
            }
        }
 
        // merge address format with the rest of the form
        array_splice($this->fields_form['input'], 3, 0, $temp_fields);
 
        return AdminController::renderForm();
    }
 
}
 
In theme/authentication.tpl , I added following code. It is added in block before 
I amended code 
<div class="account_creation">
                <h3 class="page-subheading">{l s='Your company information'}</h3>
                <p class="form-group">
                    <label for="">{l s='Company'}</label>
                    <input type="text" class="form-control" id="company" name="company" value="{if isset($smarty.post.company)}{$smarty.post.company}{/if}" />
                </p>
                <p class="form-group">
                    <label for="siret">{l s='SIRET'}</label>
                    <input type="text" class="form-control" id="siret" name="siret" value="{if isset($smarty.post.siret)}{$smarty.post.siret}{/if}" />
                </p>
                <p class="form-group">
                    <label for="ape">{l s='APE'}</label>
                    <input type="text" class="form-control" id="ape" name="ape" value="{if isset($smarty.post.ape)}{$smarty.post.ape}{/if}" />
                </p>-
                <p class="form-group">
                    <label for="website">{l s='Website'}</label>
                    <input type="text" class="form-control" id="website" name="website" value="{if isset($smarty.post.website)}{$smarty.post.website}{/if}" />
                </p>
               
            </div>

 

INTO CODE

 

<div class="account_creation">
                <h3 class="page-subheading">{l s='Your company information'}</h3>
                <p class="form-group">
                    <label for="">{l s='Company'}</label>
                    <input type="text" class="form-control" id="company" name="company" value="{if isset($smarty.post.company)}{$smarty.post.company}{/if}" />
                </p>
                <!--<p class="form-group">
                    <label for="siret">{l s='SIRET'}</label>
                    <input type="text" class="form-control" id="siret" name="siret" value="{if isset($smarty.post.siret)}{$smarty.post.siret}{/if}" />
                </p>
                <p class="form-group">
                    <label for="ape">{l s='APE'}</label>
                    <input type="text" class="form-control" id="ape" name="ape" value="{if isset($smarty.post.ape)}{$smarty.post.ape}{/if}" />
                </p>-->
                <p class="form-group">
                    <label for="website">{l s='Website'}</label>
                    <input type="text" class="form-control" id="website" name="website" value="{if isset($smarty.post.website)}{$smarty.post.website}{/if}" />
                </p>
                {foreach from=$dlv_all_fields item=field_name}
                    {if $field_name eq "vat_number"}
                        
                            <p class="form-group">
                                <label for="vat_number">{l s='VAT number'}{if in_array($field_name, $required_fields)} <sup>*</sup>{/if}</label>
                                <input type="text" class="form-control" id="vat_number" name="vat_number" value="{if isset($smarty.post.vat_number)}{$smarty.post.vat_number}{/if}" />
                            </p>
                            <p class="form-group">
                                <label for="dni">{l s='Identification number'}</label>
                                <input type="text" class="form-control" name="dni" id="dni" value="{if isset($smarty.post.dni)}{$smarty.post.dni}{/if}" />
                            </p>
                            <p class="form-group">
                                <label for="ic_dph">{l s='IC DPH'}</label>
                                <input type="text" class="form-control" name="ic_dph" id="ic_dph" value="{if isset($smarty.post.ic_dph)}{$smarty.post.ic_dph}{/if}" />
                            </p>
 
                    {/if}
                {/foreach}
            </div>
 
What I need to do in order to make 1st registration step with my current settings to create record in ps_address and save vat_number , dni and ic_dph 
 
If somebody can help I will appreciate.
 
Thank you

 

Link to comment
Share on other sites

5 hours ago, field_is_required said:

You can't do that with prestashop without hacking 90% of core unfortunately

Thank you for answer I thought that may be the only way. I do not want to override core in such a scale. I do not want break authentication steps / works perfectly /. I need to have ps_address - dni, vat_number - filled in for possible future use with other modules. / VAT number online check for example /

So that mean maybe creating own module with new  table which will holds user_id / same as in ps_address /, isUsedBool /default = false / and 3 custom_dni, custom_vat_number, custom_ic_dph.

Custom table will update on first registration step using hook.

Second hook will be watching forms connected to ps_address / dni , vat_number /. It will fill form /from very first article above/ with new table data IF isUsedBool == false

Once PrestaShop original second step /address/ registration will be completed first time in background, next hook will change isUsedBool = true;

 

That should be good enough as I need customer to fill address anyway before I can send goods out. I only want to be notified and informed that B2B customer is registered and I still want to leave new fields optional in first registration step. B2B customer will fill in these data anyway in latest on goods order and that is latest admin need to be informed about. I can still use part of code from very first article.

I will come back with my findings.

 

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