Jump to content

Custom fields in customers address - not saving in database


quendi

Recommended Posts

I have made some additional fields in the client's address (PrestaShop 1.6.1.x) - however, when entering data, the values do not enter into the database.

I added fields in the ps_address table, modified the files in the override directory (Address.php, AdminAdressessController.php).

Address.php

<?php
class Address extends AddressCore
{
 
    public $nr_faktury;
	public $symb_dok;
	public $data-wyst;
	public $termin;
	public $sposob_platnosci;
	public $opoznienie;
	public $kwota_faktury;
	public $nnaleznosc;
	public $nzobowiazanie;
	public $nn_razem;
	public $nz_razem;
	public $saldo;
 
 
    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),
            'nr_faktury' =>          array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255),
			'symb_dok' =>            array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255),
			'data-wyst' =>           array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255),
			'termin' =>              array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255),
            'sposob_platnosci' =>    array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255),
			'opoznienie' =>          array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255),
			'kwota_faktury' =>       array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255),
			'nnaleznosc' =>          array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255),
			'nzobowiazanie' =>       array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255),
			'nn_razem' =>            array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255),
			'nz_razem' =>            array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255),
			'saldo' =>               array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255),
		
 
        ),
    );
} 

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('Nr faktury'),
					'name' => 'nr_faktury',
					'required' => false,
					
				),
				array(
    'type' => 'text',
    'label' => $this->l('Symbol dokumentu'),
    'name' => 'symb_dok',
    'required' => false,
)
),
				array(
    'type' => 'text',
    'label' => $this->l('Data wystawienia'),
    'name' => 'data_wyst',
    'required' => false,
)
),
				array(
    'type' => 'text',
    'label' => $this->l('Termin płatności'),
    'name' => 'termin',
    'required' => false,
)
),
				array(
    'type' => 'text',
    'label' => $this->l('Sposób płatności'),
    'name' => 'sposob_platnosci',
    'required' => false,
)
),
				array(
    'type' => 'text',
    'label' => $this->l('Opóźnienie'),
    'name' => 'opoznienie',
    'required' => false,
)
),
				array(
    'type' => 'text',
    'label' => $this->l('Kwota faktury'),
    'name' => 'kwota_faktury',
    'required' => false,
)
),
				array(
    'type' => 'text',
    'label' => $this->l('Niezapłacona należność'),
    'name' => 'nnaleznosc',
    'required' => false,
)
),
				array(
    'type' => 'text',
    'label' => $this->l('Niezapłacone zobowiązanie'),
    'name' => 'nzobowiazanie',
    'required' => false,
)
),
				array(
    'type' => 'text',
    'label' => $this->l('Razem należność'),
    'name' => 'nn_razem',
    'required' => false,
)
),
				array(
    'type' => 'text',
    'label' => $this->l('Razem zobowiazanie'),
    'name' => 'nz_razem',
    'required' => false,
)
),
				array(
    'type' => 'text',
    'label' => $this->l('Saldo'),
    'name' => 'saldo',
    'required' => false,
)
),
                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();
    }
     
}

 

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