<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NemoPS &#187; new fields</title>
	<atom:link href="http://nemops.com/tag/new-fields/feed/" rel="self" type="application/rss+xml" />
	<link>http://nemops.com</link>
	<description>Prestashop Tutorials, Modules and More!</description>
	<lastBuildDate>Wed, 05 Dec 2018 13:25:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.35</generator>
	<item>
		<title>How to add new fields to the customer address in Prestashop</title>
		<link>http://nemops.com/new-customer-address-fields-prestashop/</link>
		<comments>http://nemops.com/new-customer-address-fields-prestashop/#comments</comments>
		<pubDate>Tue, 22 Apr 2014 09:21:30 +0000</pubDate>
		<dc:creator><![CDATA[Nemo]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[new fields]]></category>
		<category><![CDATA[overrides]]></category>
		<category><![CDATA[prestashop 1.6]]></category>
		<category><![CDATA[registration]]></category>

		<guid isPermaLink="false">http://nemops.com/?p=1949</guid>
		<description><![CDATA[<p>In today&#8217;s tutorial, we will see how to add new fields to the customer address in Prestashop. UPDATE! If you prefer a simple solution, I created a module that automates the process and does much more, find it here Version used: Prestashop 1.6 (compatible with Prestashop 1.5) Plan of action In order to add a [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://nemops.com/new-customer-address-fields-prestashop/">How to add new fields to the customer address in Prestashop</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>In today&#8217;s tutorial, we will see how to add new fields to the customer address in Prestashop.</p>
<p><span id="more-1949"></span></p>
<p><strong>UPDATE! If you prefer a simple solution, I created a module that automates the process and does much more, find it <a href="http://store.nemops.com/modules/45-prestashop-address-fields.html#.WhvflkqWaUk" title="PrestaShop Address Fields Module">here</a></strong></p>
<a class="download-files button style1" href="http://nemops.com/wp-content/uploads/2014/04/new_registration_fields.zip" title="Download Project Files">Download Project Files</a>
<ul>
<li>Version used: Prestashop 1.6 (compatible with Prestashop 1.5)</li>
</ul>
<h2>Plan of action</h2>
<p>In order to add a new field to the address registration form, we will be adding a couple of overrides, as well as creating a new database field and modifying the address template file. If you are not familiar with Prestashop overrides, check out my article on <a href="http://nemops.com/extending-prestashop-objects/#.U1WG4Pna6r0" title="how to extend Prestashop objects" target="_blank">how to extend Prestashop objects</a> (although we will have to use a slightly different technique).</p>
<div class="separator"></div>
<h2>The address class and database table</h2>
<p>First things first, we need something to play with. We want the customer to be able to fill in a field which doesn&#8217;t currently exist, therefore let&#8217;s login to the database, access the <strong>ps_address</strong> table and create a new field: <strong>my_custom_field</strong>. I made it a 64 long VARCHAR field.</p>
<p>Next we need to tell the address object (class) that it has a new field to deal with. Create a new file inside <em>override/classes/</em> and call it <strong>Address.php</strong>. Add the following inside php tags:</p>
<pre class="brush: php; title: ; notranslate">



class Address extends AddressCore
{

	public $my_custom_field;


	public static $definition = array(
		'table' =&gt; 'address',
		'primary' =&gt; 'id_address',
		'fields' =&gt; array(
			'id_customer' =&gt; 		array('type' =&gt; self::TYPE_INT, 'validate' =&gt; 'isNullOrUnsignedId', 'copy_post' =&gt; false),
			'id_manufacturer' =&gt; 	   array('type' =&gt; self::TYPE_INT, 'validate' =&gt; 'isNullOrUnsignedId', 'copy_post' =&gt; false),
			'id_supplier' =&gt; 		array('type' =&gt; self::TYPE_INT, 'validate' =&gt; 'isNullOrUnsignedId', 'copy_post' =&gt; false),
			'id_warehouse' =&gt; 		array('type' =&gt; self::TYPE_INT, 'validate' =&gt; 'isNullOrUnsignedId', 'copy_post' =&gt; false),
			'id_country' =&gt; 		array('type' =&gt; self::TYPE_INT, 'validate' =&gt; 'isUnsignedId', 'required' =&gt; true),
			'id_state' =&gt; 			array('type' =&gt; self::TYPE_INT, 'validate' =&gt; 'isNullOrUnsignedId'),
			'alias' =&gt; 				array('type' =&gt; self::TYPE_STRING, 'validate' =&gt; 'isGenericName', 'required' =&gt; true, 'size' =&gt; 32),
			'company' =&gt; 			array('type' =&gt; self::TYPE_STRING, 'validate' =&gt; 'isGenericName', 'size' =&gt; 64),
			'lastname' =&gt; 			array('type' =&gt; self::TYPE_STRING, 'validate' =&gt; 'isName', 'required' =&gt; true, 'size' =&gt; 32),
			'firstname' =&gt; 			array('type' =&gt; self::TYPE_STRING, 'validate' =&gt; 'isName', 'required' =&gt; true, 'size' =&gt; 32),
			'vat_number' =&gt;	 		array('type' =&gt; self::TYPE_STRING, 'validate' =&gt; 'isGenericName'),
			'address1' =&gt; 			array('type' =&gt; self::TYPE_STRING, 'validate' =&gt; 'isAddress', 'required' =&gt; true, 'size' =&gt; 128),
			'address2' =&gt; 			array('type' =&gt; self::TYPE_STRING, 'validate' =&gt; 'isAddress', 'size' =&gt; 128),
			'postcode' =&gt; 			array('type' =&gt; self::TYPE_STRING, 'validate' =&gt; 'isPostCode', 'size' =&gt; 12),
			'city' =&gt; 				array('type' =&gt; self::TYPE_STRING, 'validate' =&gt; 'isCityName', 'required' =&gt; true, 'size' =&gt; 64),
			'other' =&gt; 				array('type' =&gt; self::TYPE_STRING, 'validate' =&gt; 'isMessage', 'size' =&gt; 300),
			'phone' =&gt; 				array('type' =&gt; self::TYPE_STRING, 'validate' =&gt; 'isPhoneNumber', 'size' =&gt; 32),
			'phone_mobile' =&gt; 		array('type' =&gt; self::TYPE_STRING, 'validate' =&gt; 'isPhoneNumber', 'size' =&gt; 32),
			'dni' =&gt; 				array('type' =&gt; self::TYPE_STRING, 'validate' =&gt; 'isDniLite', 'size' =&gt; 16),
			'deleted' =&gt; 			array('type' =&gt; self::TYPE_BOOL, 'validate' =&gt; 'isBool', 'copy_post' =&gt; false),
			'date_add' =&gt; 			array('type' =&gt; self::TYPE_DATE, 'validate' =&gt; 'isDateFormat', 'copy_post' =&gt; false),
			'date_upd' =&gt; 			array('type' =&gt; self::TYPE_DATE, 'validate' =&gt; 'isDateFormat', 'copy_post' =&gt; false),
			'my_custom_field' =&gt; 	array('type' =&gt; self::TYPE_STRING, 'validate' =&gt; 'isGenericName', 'size' =&gt; 64),

		),
	);
}
</pre>
<p><strong>Explanation:</strong> if you read my article on how to extend Prestashop Objects, you will notice I approached this override a little differently. I am not assigning the new field upon instance creation, but <strong>I am directly overriding its definition</strong>. The reason is simple: the Address definition is called statically when creating a new address in the front office, and if we added the new field in the construct method, we would have missed it from the list.</p>
<p>Please notice that this definition comes from <strong>Prestashop 1.6.0.5</strong>, and I therefore recommend to use your own from the original Address class, in case you have a different Prestashop version.</p>
<div class="separator"></div>
<h2>The back office Address Controller</h2>
<p>We are half the way through already, and at this point we need to override the back office controller responsible for displaying the address modification form for each customer.</p>
<p>Create a new file named <strong>AdminAddressesController.php</strong> in <em>override/controllers/admin/</em>. We need to extend the renderForm() method; thus, head over the main <em>controllers/admin</em> folder, open up the original <strong>AdminAddressesController.php</strong>, locate the <strong>renderForm()</strong> method and copy it. Then, add the following inside php tags in our <strong>new</strong> controller override:</p>
<pre class="brush: php; title: ; notranslate">

Class AdminAddressesController extends AdminAddressesControllerCore
{

}

</pre>
<p>And paste the renderForm() code inside the class. Here is how mine looks like, taken from Prestashop 1.6.0.5:</p>
<pre class="brush: php; title: ; notranslate">

Class AdminAddressesController extends AdminAddressesControllerCore
{


	public function renderForm()
	{
		$this-&gt;fields_form = array(
			'legend' =&gt; array(
				'title' =&gt; $this-&gt;l('Addresses'),
				'icon' =&gt; 'icon-envelope-alt'
			),
			'input' =&gt; array(
				array(
					'type' =&gt; 'text_customer',
					'label' =&gt; $this-&gt;l('Customer'),
					'name' =&gt; 'id_customer',
					'required' =&gt; false,
				),
				array(
					'type' =&gt; 'text',
					'label' =&gt; $this-&gt;l('Identification Number'),
					'name' =&gt; 'dni',
					'required' =&gt; false,
					'col' =&gt; '4',
					'hint' =&gt; $this-&gt;l('DNI / NIF / NIE')
				),			
				array(
					'type' =&gt; 'text',
					'label' =&gt; $this-&gt;l('Address alias'),
					'name' =&gt; 'alias',
					'required' =&gt; true,
					'col' =&gt; '4',
					'hint' =&gt; $this-&gt;l('Invalid characters:').' &amp;lt;&amp;gt;;=#{}'
				),
				array(
					'type' =&gt; 'text',
					'label' =&gt; $this-&gt;l('Home phone'),
					'name' =&gt; 'phone',
					'required' =&gt; false,
					'col' =&gt; '4',
					'hint' =&gt; Configuration::get('PS_ONE_PHONE_AT_LEAST') ? sprintf($this-&gt;l('You must register at least one phone number.')) : ''
				),
				array(
					'type' =&gt; 'text',
					'label' =&gt; $this-&gt;l('Mobile phone'),
					'name' =&gt; 'phone_mobile',
					'required' =&gt; false,
					'col' =&gt; '4',
					'hint' =&gt; Configuration::get('PS_ONE_PHONE_AT_LEAST') ? sprintf($this-&gt;l('You must register at least one phone number.')) : ''
				),
				array(
					'type' =&gt; 'textarea',
					'label' =&gt; $this-&gt;l('Other'),
					'name' =&gt; 'other',
					'required' =&gt; false,
					'cols' =&gt; 15,
					'rows' =&gt; 3,
					'hint' =&gt; $this-&gt;l('Forbidden characters:').' &amp;lt;&amp;gt;;=#{}'
				),
			),
			'submit' =&gt; array(
				'title' =&gt; $this-&gt;l('Save'),
			)
		);
		$id_customer = (int)Tools::getValue('id_customer');
		if (!$id_customer &amp;&amp; Validate::isLoadedObject($this-&gt;object))
			$id_customer = $this-&gt;object-&gt;id_customer;
		if ($id_customer)
		{
			$customer = new Customer((int)$id_customer);
			$token_customer = Tools::getAdminToken('AdminCustomers'.(int)(Tab::getIdFromClassName('AdminCustomers')).(int)$this-&gt;context-&gt;employee-&gt;id);
		}

		$this-&gt;tpl_form_vars = array(
			'customer' =&gt; isset($customer) ? $customer : null,
			'tokenCustomer' =&gt; isset ($token_customer) ? $token_customer : null
		);

		// Order address fields depending on country format
		$addresses_fields = $this-&gt;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' =&gt; 'text',
					'label' =&gt; $this-&gt;l('Company'),
					'name' =&gt; 'company',
					'required' =&gt; false,
					'col' =&gt; '4',
					'hint' =&gt; $this-&gt;l('Invalid characters:').' &amp;lt;&amp;gt;;=#{}'
				);
				$temp_fields[] = array(
					'type' =&gt; 'text',
					'label' =&gt; $this-&gt;l('VAT number'),
					'col' =&gt; '2',
					'name' =&gt; 'vat_number'
				);
			}
			else if ($addr_field_item == 'lastname')
			{
				if (isset($customer) &amp;&amp;
					!Tools::isSubmit('submit'.strtoupper($this-&gt;table)) &amp;&amp;
					Validate::isLoadedObject($customer) &amp;&amp;
					!Validate::isLoadedObject($this-&gt;object))
					$default_value = $customer-&gt;lastname;
				else
					$default_value = '';

				$temp_fields[] = array(
					'type' =&gt; 'text',
					'label' =&gt; $this-&gt;l('Last Name'),
					'name' =&gt; 'lastname',
					'required' =&gt; true,
					'col' =&gt; '4',
					'hint' =&gt; $this-&gt;l('Invalid characters:').' 0-9!&amp;amp;lt;&amp;amp;gt;,;?=+()@#&quot;�{}_$%:',
					'default_value' =&gt; $default_value,
				);
			}
			else if ($addr_field_item == 'firstname')
			{
				if (isset($customer) &amp;&amp;
					!Tools::isSubmit('submit'.strtoupper($this-&gt;table)) &amp;&amp;
					Validate::isLoadedObject($customer) &amp;&amp;
					!Validate::isLoadedObject($this-&gt;object))
					$default_value = $customer-&gt;firstname;
 	 	 	 	else
 	 	 	 		$default_value = '';

				$temp_fields[] = array(
					'type' =&gt; 'text',
					'label' =&gt; $this-&gt;l('First Name'),
					'name' =&gt; 'firstname',
					'required' =&gt; true,
					'col' =&gt; '4',
					'hint' =&gt; $this-&gt;l('Invalid characters:').' 0-9!&amp;amp;lt;&amp;amp;gt;,;?=+()@#&quot;�{}_$%:',
					'default_value' =&gt; $default_value,
				);
			}
			else if ($addr_field_item == 'address1')
			{
				$temp_fields[] = array(
					'type' =&gt; 'text',
					'label' =&gt; $this-&gt;l('Address'),
					'name' =&gt; 'address1',
					'col' =&gt; '6',
					'required' =&gt; true,
				);
			}
			else if ($addr_field_item == 'address2')
			{
				$temp_fields[] = array(
					'type' =&gt; 'text',
					'label' =&gt; $this-&gt;l('Address').' (2)',
					'name' =&gt; 'address2',
					'col' =&gt; '6',
					'required' =&gt; false,
				);
			}
			elseif ($addr_field_item == 'postcode')
			{
				$temp_fields[] = array(
					'type' =&gt; 'text',
					'label' =&gt; $this-&gt;l('Zip/Postal Code'),
					'name' =&gt; 'postcode',
					'col' =&gt; '2',
					'required' =&gt; true,
				);
			}
			else if ($addr_field_item == 'city')
			{
				$temp_fields[] = array(
					'type' =&gt; 'text',
					'label' =&gt; $this-&gt;l('City'),
					'name' =&gt; 'city',
					'col' =&gt; '4',
					'required' =&gt; true,
				);
			}
			else if ($addr_field_item == 'country' || $addr_field_item == 'Country:name')
			{
				$temp_fields[] = array(
					'type' =&gt; 'select',
					'label' =&gt; $this-&gt;l('Country'),
					'name' =&gt; 'id_country',
					'required' =&gt; false,
					'col' =&gt; '4',
					'default_value' =&gt; (int)$this-&gt;context-&gt;country-&gt;id,
					'options' =&gt; array(
						'query' =&gt; Country::getCountries($this-&gt;context-&gt;language-&gt;id),
						'id' =&gt; 'id_country',
						'name' =&gt; 'name'
					)
				);
				$temp_fields[] = array(
					'type' =&gt; 'select',
					'label' =&gt; $this-&gt;l('State'),
					'name' =&gt; 'id_state',
					'required' =&gt; false,
					'col' =&gt; '4',
					'options' =&gt; array(
						'query' =&gt; array(),
						'id' =&gt; 'id_state',
						'name' =&gt; 'name'
					)
				);
			}
		}

		// merge address format with the rest of the form
		array_splice($this-&gt;fields_form['input'], 3, 0, $temp_fields);

		return parent::renderForm();
	}
	
}

</pre>
<p>We must edit the $this->fields_form variable to add a new input field; right after this:</p>
<pre class="brush: php; title: ; notranslate">
				array(
					'type' =&gt; 'text',
					'label' =&gt; $this-&gt;l('Identification Number'),
					'name' =&gt; 'dni',
					'required' =&gt; false,
					'col' =&gt; '4',
					'hint' =&gt; $this-&gt;l('DNI / NIF / NIE')
				),	
</pre>
<p>Add the following</p>
<pre class="brush: php; title: ; notranslate">
				array(
					'type' =&gt; 'text',
					'label' =&gt; $this-&gt;l('My custom field'),
					'name' =&gt; 'my_custom_field',
					'required' =&gt; false,
					'col' =&gt; '4',
					'hint' =&gt; $this-&gt;l('Just a custom field!')
				),	
</pre>
<p><strong>Note:</strong> make sure the &#8220;name&#8221; matches the one of the new attribute previously added to both the database and Address class object!</p>
<p>Lastly, at the end of the method, change:</p>
<pre class="brush: php; title: ; notranslate">
return parent::renderForm();
</pre>
<p>To</p>
<pre class="brush: php; title: ; notranslate">
return AdminController::renderForm();
</pre>
<p>So that the new fields list is not replaced by the original one.</p>
<p>If the Address class or/and controller were not previously overridden, at this point you must reach	the <em>cache/</em> folder and erase <strong>class_index.php</strong>; as always. After doing it, overrides will take place.</p>
<div class="separator"></div>
<h2>The Front Office Address Template</h2>
<p>As very last step, let&#8217;s now add the custom field to the front office template, so it can be filled in by clients when they register. Open up <strong>address.tpl</strong>, located in the theme folder. I am using the default bootstrap template, so your file might look differently in case you have a custom one! Locate:</p>
<pre class="brush: php; html-script: true; title: ; notranslate">

			{if $field_name eq 'vat_number'}
				&lt;div id=&quot;vat_area&quot;&gt;
					&lt;div id=&quot;vat_number&quot;&gt;
						&lt;div class=&quot;form-group&quot;&gt;
							&lt;label for=&quot;vat-number&quot;&gt;{l s='VAT number'}&lt;/label&gt;
							&lt;input type=&quot;text&quot; class=&quot;form-control validate&quot; data-validate=&quot;{$address_validation.$field_name.validate}&quot; id=&quot;vat-number&quot; name=&quot;vat_number&quot; value=&quot;{if isset($smarty.post.vat_number)}{$smarty.post.vat_number}{else}{if isset($address-&gt;vat_number)}{$address-&gt;vat_number|escape:'html':'UTF-8'}{/if}{/if}&quot; /&gt;
						&lt;/div&gt;
					&lt;/div&gt;
				&lt;/div&gt;
			{/if}
</pre>
<p>And right after it, add:</p>
<pre class="brush: php; html-script: true; title: ; notranslate">

			{if $field_name eq 'my_custom_field'}
				&lt;div id=&quot;vat_area&quot;&gt;
					&lt;div id=&quot;my_custom_field&quot;&gt;
						&lt;div class=&quot;form-group&quot;&gt;
							&lt;label for=&quot;my-custom-field&quot;&gt;{l s='My Custom Field'}&lt;/label&gt;
							&lt;input type=&quot;text&quot; class=&quot;form-control validate&quot; data-validate=&quot;{$address_validation.$field_name.validate}&quot; id=&quot;my-custom-field&quot; name=&quot;my_custom_field&quot; value=&quot;{if isset($smarty.post.my_custom_field)}{$smarty.post.my_custom_field}{else}{if isset($address-&gt;my_custom_field)}{$address-&gt;my_custom_field|escape:'html':'UTF-8'}{/if}{/if}&quot; /&gt;
						&lt;/div&gt;
					&lt;/div&gt;
				&lt;/div&gt;
			{/if}
</pre>
<p>Save and refresh, we&#8217;re done!</p>
<p><strong>TIP:</strong> if you don&#8217;t see any change in the front office, make sure you turn on recompilation from <em>Advanced Parameters > Performance</em>, and clear Smarty cache as well</p>
<div class="separator"></div>
<h2>Optional: Making the new field mandatory</h2>
<p>If you really need your customers to input something in that new field, than another override is necessary. Create a new file within <em>override/controllers/front</em> and call it <strong>AddressController.php</strong>. Paste the following inside php tags, ad always:</p>
<pre class="brush: php; title: ; notranslate">

Class AddressController extends AddressControllerCore
{

}
</pre>
<p>We need to override the <strong>processSubmitAddress()</strong> method. Therefore, if your Prestashop version is not 1.6.0.5, go ahead and copy your original one inside this new override; otherwise you can use the following:</p>
<pre class="brush: php; title: ; notranslate">

	protected function processSubmitAddress()
	{
		$address = new Address();
		$this-&gt;errors = $address-&gt;validateController();
		$address-&gt;id_customer = (int)$this-&gt;context-&gt;customer-&gt;id;

		// Check page token
		if ($this-&gt;context-&gt;customer-&gt;isLogged() &amp;&amp; !$this-&gt;isTokenValid())
			$this-&gt;errors[] = Tools::displayError('Invalid token.');

		// Check phone
		if (Configuration::get('PS_ONE_PHONE_AT_LEAST') &amp;&amp; !Tools::getValue('phone') &amp;&amp; !Tools::getValue('phone_mobile'))
			$this-&gt;errors[] = Tools::displayError('You must register at least one phone number.');
		if ($address-&gt;id_country)
		{
			// Check country
			if (!($country = new Country($address-&gt;id_country)) || !Validate::isLoadedObject($country))
				throw new PrestaShopException('Country cannot be loaded with address-&gt;id_country');

			if ((int)$country-&gt;contains_states &amp;&amp; !(int)$address-&gt;id_state)
				$this-&gt;errors[] = Tools::displayError('This country requires you to chose a State.');

			$postcode = Tools::getValue('postcode');		
			/* Check zip code format */
			if ($country-&gt;zip_code_format &amp;&amp; !$country-&gt;checkZipCode($postcode))
				$this-&gt;errors[] = sprintf(Tools::displayError('The Zip/Postal code you\'ve entered is invalid. It must follow this format: %s'), str_replace('C', $country-&gt;iso_code, str_replace('N', '0', str_replace('L', 'A', $country-&gt;zip_code_format))));
			elseif(empty($postcode) &amp;&amp; $country-&gt;need_zip_code)
				$this-&gt;errors[] = Tools::displayError('A Zip/Postal code is required.');
			elseif ($postcode &amp;&amp; !Validate::isPostCode($postcode))
				$this-&gt;errors[] = Tools::displayError('The Zip/Postal code is invalid.');

			// Check country DNI
			if ($country-&gt;isNeedDni() &amp;&amp; (!Tools::getValue('dni') || !Validate::isDniLite(Tools::getValue('dni'))))
				$this-&gt;errors[] = Tools::displayError('The identification number is incorrect or has already been used.');
			else if (!$country-&gt;isNeedDni())
				$address-&gt;dni = null;
		}
		// Check if the alias exists
		if (!$this-&gt;context-&gt;customer-&gt;is_guest &amp;&amp; !empty($_POST['alias']) &amp;&amp; (int)$this-&gt;context-&gt;customer-&gt;id &gt; 0)
		{
			$id_address = Tools::getValue('id_address');
			if(Configuration::get('PS_ORDER_PROCESS_TYPE') &amp;&amp; (int)Tools::getValue('opc_id_address_'.Tools::getValue('type')) &gt; 0)
				$id_address = Tools::getValue('opc_id_address_'.Tools::getValue('type'));
 	
			if (Db::getInstance()-&gt;getValue('
				SELECT count(*)
				FROM '._DB_PREFIX_.'address
				WHERE `alias` = \''.pSql($_POST['alias']).'\'
				AND id_address != '.(int)$id_address.'
				AND id_customer = '.(int)$this-&gt;context-&gt;customer-&gt;id.'
				AND deleted = 0') &gt; 0)
				$this-&gt;errors[] = sprintf(Tools::displayError('The alias &quot;%s&quot; has already been used. Please select another one.'), Tools::safeOutput($_POST['alias']));
		}

		// Check the requires fields which are settings in the BO
		$this-&gt;errors = array_merge($this-&gt;errors, $address-&gt;validateFieldsRequiredDatabase());

		// Don't continue this process if we have errors !
		if ($this-&gt;errors &amp;&amp; !$this-&gt;ajax)
			return;

		// If we edit this address, delete old address and create a new one
		if (Validate::isLoadedObject($this-&gt;_address))
		{
			if (Validate::isLoadedObject($country) &amp;&amp; !$country-&gt;contains_states)
				$address-&gt;id_state = 0;
			$address_old = $this-&gt;_address;
			if (Customer::customerHasAddress($this-&gt;context-&gt;customer-&gt;id, (int)$address_old-&gt;id))
			{
				if ($address_old-&gt;isUsed())
					$address_old-&gt;delete();
				else
				{
					$address-&gt;id = (int)($address_old-&gt;id);
					$address-&gt;date_add = $address_old-&gt;date_add;
				}
			}
		}
		
		if ($this-&gt;ajax &amp;&amp; Tools::getValue('type') == 'invoice' &amp;&amp; Configuration::get('PS_ORDER_PROCESS_TYPE'))
		{
			$this-&gt;errors = array_unique(array_merge($this-&gt;errors, $address-&gt;validateController()));			
			if (count($this-&gt;errors))
			{
				$return = array(
					'hasError' =&gt; (bool)$this-&gt;errors,
					'errors' =&gt; $this-&gt;errors
				);
				die(Tools::jsonEncode($return));
			}
		}
		
		// Save address
		if ($result = $address-&gt;save())
		{			
			// Update id address of the current cart if necessary
			if (isset($address_old) &amp;&amp; $address_old-&gt;isUsed())
				$this-&gt;context-&gt;cart-&gt;updateAddressId($address_old-&gt;id, $address-&gt;id);
			else // Update cart address
				$this-&gt;context-&gt;cart-&gt;autosetProductAddress();

			if ((bool)(Tools::getValue('select_address', false)) == true OR (Tools::getValue('type') == 'invoice' &amp;&amp; Configuration::get('PS_ORDER_PROCESS_TYPE')))
				$this-&gt;context-&gt;cart-&gt;id_address_invoice = (int)$address-&gt;id;
			elseif (Configuration::get('PS_ORDER_PROCESS_TYPE'))
				$this-&gt;context-&gt;cart-&gt;id_address_invoice = (int)$this-&gt;context-&gt;cart-&gt;id_address_delivery;
			$this-&gt;context-&gt;cart-&gt;update();

			if ($this-&gt;ajax)
			{
				$return = array(
					'hasError' =&gt; (bool)$this-&gt;errors,
					'errors' =&gt; $this-&gt;errors,
					'id_address_delivery' =&gt; (int)$this-&gt;context-&gt;cart-&gt;id_address_delivery,
					'id_address_invoice' =&gt; (int)$this-&gt;context-&gt;cart-&gt;id_address_invoice
				);
				die(Tools::jsonEncode($return));
			}

			// Redirect to old page or current page
			if ($back = Tools::getValue('back'))
			{
				if ($back == Tools::secureReferrer(Tools::getValue('back')))
					Tools::redirect(html_entity_decode($back));
				$mod = Tools::getValue('mod');
				Tools::redirect('index.php?controller='.$back.($mod ? '&amp;back='.$mod : ''));
			}
			else
				Tools::redirect('index.php?controller=addresses');
		}		
		$this-&gt;errors[] = Tools::displayError('An error occurred while updating your address.');
	}
</pre>
<p>Right before the // CHeck Phone comment, add:</p>
<pre class="brush: php; title: ; notranslate">
		if ( !Tools::getValue('my_custom_field'))
			$this-&gt;errors[] = Tools::displayError('The custom field is mandatory!');
</pre>
<p>So that an error will be thrown if your clients leave that field empty!</p>
<p>Remember to erase the class index file again, of course.</p>
<div class="separator"></div>
<h2>Adding the new field to the address format in Prestashop</h2>
<p>As a side note, be aware that in order to display the new field we added we need to change the address format for each country. I am not currently aware of a simple way to apply address changes to all countries at once. Therefore, head over to <strong>Localization > Countries</strong>, click on a country name and add the new field to the address box:</p>
<p><a href="http://nemops.com/wp-content/uploads/2014/04/new_registration_fields_bo.jpg"><img src="http://nemops.com/wp-content/uploads/2014/04/new_registration_fields_bo-680x216.jpg" alt="Adding the new field to the address format in Prestashop" width="680" height="216" class="aligncenter size-large wp-image-1954" /></a></p>
<div class="separator"></div>
<h2>What for new Customer Fields?</h2>
<p>Throughout this tutorial, we added a new field to the Address object. However, it is also possible to create a new basic customer field, to sit along name, lastname and email in the very basic registration (even without an address). To do so, you can simply apply the same process to the <strong>Customer</strong> class, and relative controllers. Just be aware that you will need to edit a couple of different template files: <strong>identity.tpl</strong> and <strong>authentication.tpl</strong>, as well as <strong>order-opc-new-account.tpl</strong> if you use the one page checkout!</p>
<div class="separator"></div>
<p><strong>UPDATE! If you prefer a simple solution, I created a module that automates the process and does much more, find it <a href="http://store.nemops.com/modules/45-prestashop-address-fields.html#.WhvflkqWaUk" title="PrestaShop Address Fields Module">here</a></strong></p>
<h3>Additional Resources</h3>
<ul>
<li>
		<a href="http://nemops.com/extending-prestashop-objects/#.U1WG4Pna6r0" title="How to extend Prestashop objects" target="_blank">How to extend Prestashop objects</a>
	</li>
</ul>
<p>The post <a rel="nofollow" href="http://nemops.com/new-customer-address-fields-prestashop/">How to add new fields to the customer address in Prestashop</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nemops.com/new-customer-address-fields-prestashop/feed/</wfw:commentRss>
		<slash:comments>60</slash:comments>
		</item>
	</channel>
</rss>
