Merge pull request #1803 from Azhar-Webkul/gli-3085

Added : Nationality field for customers in the back-office and front-office.
This commit is contained in:
Rohit Jain
2026-09-03 12:05:46 +05:30
committed by GitHub
14 changed files with 213 additions and 2 deletions

View File

@@ -72,6 +72,12 @@
</p>
</div>
</div>
<div class="row">
<label class="control-label col-lg-3">{l s='Nationality'}</label>
<div class="col-lg-9">
<p class="form-control-static">{if $nationality}{$nationality}{else}--{/if}</p>
</div>
</div>
<div class="row">
<label class="control-label col-lg-3">{l s='Registration Date'}</label>
<div class="col-lg-9">

View File

@@ -41,6 +41,9 @@ class CustomerCore extends ObjectModel
/** @var int Gender ID */
public $id_gender = 0;
/** @var int Nationality, stored as a country ID */
public $id_country = 0;
/** @var int Default group ID */
public $id_default_group;
@@ -170,6 +173,7 @@ class CustomerCore extends ObjectModel
'passwd' => array('type' => self::TYPE_STRING, 'validate' => 'isPasswd', 'required' => true, 'size' => 60),
'last_passwd_gen' => array('type' => self::TYPE_STRING, 'copy_post' => false),
'id_gender' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_country' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'birthday' => array('type' => self::TYPE_DATE, 'validate' => 'isBirthDate'),
'newsletter' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'newsletter_date_add' => array('type' => self::TYPE_DATE,'copy_post' => false),
@@ -209,6 +213,10 @@ class CustomerCore extends ObjectModel
if (Configuration::get('PS_ONE_PHONE_AT_LEAST')) {
self::$definition['fields']['phone']['required'] = true;
}
if (Configuration::get('PS_CUSTOMER_NATIONALITY') && Configuration::get('PS_CUSTOMER_NATIONALITY_MANDATORY')) {
self::$definition['fields']['id_country']['required'] = true;
}
}
public function validateFields($die = true, $error_return = false)
@@ -830,6 +838,16 @@ class CustomerCore extends ObjectModel
return (int) Configuration::get('PS_COUNTRY_DEFAULT');
}
public static function getNationalities($id_lang)
{
$nationalities = Country::getCountries((int) $id_lang);
$nationalities[-1] = array(
'id_country' => -1,
'name' => Translate::getAdminTranslation('Other', 'AdminCustomers'),
);
return $nationalities;
}
public function toggleStatus()
{

View File

@@ -107,6 +107,20 @@ class AdminCustomerPreferencesControllerCore extends AdminController
'cast' => 'intval',
'type' => 'bool'
),
'PS_CUSTOMER_NATIONALITY' => array(
'title' => $this->l('Enable nationality'),
'hint' => $this->l('Display or not the nationality field.'),
'validation' => 'isBool',
'cast' => 'intval',
'type' => 'bool'
),
'PS_CUSTOMER_NATIONALITY_MANDATORY' => array(
'title' => $this->l('Nationality is mandatory'),
'hint' => $this->l('If you choose yes, your customer will have to select their nationality.'),
'validation' => 'isBool',
'cast' => 'intval',
'type' => 'bool'
),
'PS_CUSTOMER_NWSL' => array(
'title' => $this->l('Enable newsletter registration'),
'hint' => $this->l('Display or not the newsletter registration tick box.'),

View File

@@ -77,12 +77,39 @@ class AdminCustomersControllerCore extends AdminController
$groups_array[$group['id_group']] = $group['name'];
}
$selected_nationalities = array();
$nationalities_result = Db::getInstance()->executeS('
SELECT DISTINCT cl.id_country, cl.name
FROM '._DB_PREFIX_.'customer c
INNER JOIN '._DB_PREFIX_.'country_lang cl ON (cl.id_country = c.id_country AND cl.id_lang = '.(int)$this->context->language->id.')
WHERE c.id_country != 0 AND c.deleted = 0
ORDER BY cl.name ASC
');
foreach ($nationalities_result as $row) {
$selected_nationalities[$row['id_country']] = $row['name'];
}
$selected_nationalities[-1] = $this->l('Other');
$selected_nationalities[0] = $this->l('None');
$selected_countries = array();
$countries_result = Db::getInstance()->executeS('
SELECT DISTINCT cl.id_country, cl.name
FROM '._DB_PREFIX_.'address ad
INNER JOIN '._DB_PREFIX_.'country_lang cl ON (cl.id_country = ad.id_country AND cl.id_lang = '.(int)$this->context->language->id.')
WHERE ad.deleted = 0
ORDER BY cl.name ASC
');
foreach ($countries_result as $row) {
$selected_countries[$row['id_country']] = $row['name'];
}
$this->_join = 'LEFT JOIN '._DB_PREFIX_.'gender_lang gl ON (a.id_gender = gl.id_gender AND gl.id_lang = '.(int)$this->context->language->id.')';
$this->_join .= ' LEFT JOIN '._DB_PREFIX_.'group_lang grl ON (a.id_default_group = grl.id_group AND grl.id_lang = '.(int)$this->context->language->id.')';
$this->_join .= ' LEFT JOIN '._DB_PREFIX_.'orders o ON (a.id_customer = o.id_customer)';
$this->_join .= ' LEFT JOIN '._DB_PREFIX_.'address ad ON (ad.id_customer = a.id_customer AND ad.deleted = 0)';
$this->_join .= ' LEFT JOIN '._DB_PREFIX_.'state adstate ON (adstate.id_state = ad.id_state)';
$this->_join .= ' LEFT JOIN '._DB_PREFIX_.'country_lang adcountry ON (adcountry.id_country = ad.id_country AND adcountry.id_lang = '.(int)$this->context->language->id.')';
$this->_join .= ' LEFT JOIN '._DB_PREFIX_.'country_lang natcountry ON (natcountry.id_country = a.id_country AND natcountry.id_lang = '.(int)$this->context->language->id.')';
$this->_group = 'GROUP BY a.`id_customer`';
$this->fields_list = array(
@@ -134,6 +161,16 @@ class AdminCustomersControllerCore extends AdminController
'list' => $groups_array,
'filter_key' => 'a!id_default_group',
),
'nationality' => array(
'title' => $this->l('Nationality'),
'optional' => true,
'visible_default' => false,
'type' => 'select',
'list' => $selected_nationalities,
'filter_key' => 'a!id_country',
'filter_type' => 'int',
'orderby' => false,
),
'total_orders' => array(
'title' => $this->l('Number of orders'),
'type' => 'range',
@@ -205,7 +242,10 @@ class AdminCustomersControllerCore extends AdminController
'title' => $this->l('Country'),
'optional' => true,
'visible_default' => false,
'havingFilter' => true,
'type' => 'select',
'list' => $selected_countries,
'filter_key' => 'ad!id_country',
'filter_type' => 'int',
'orderby' => false,
),
'address_phone' => array(
@@ -299,7 +339,8 @@ class AdminCustomersControllerCore extends AdminController
ad.company as address_company, ad.vat_number,
ad.address1, ad.address2, ad.city, adstate.name as state,
ad.postcode, adcountry.name as country, ad.phone as address_phone,
ad.phone_mobile, ad.dni';
ad.phone_mobile, ad.dni,
(CASE WHEN a.id_country = -1 THEN \''.pSQL($this->l('Other')).'\' ELSE natcountry.name END) as nationality';
// Check if we can add a customer
if (Shop::isFeatureActive() && (Shop::getContext() == Shop::CONTEXT_ALL || Shop::getContext() == Shop::CONTEXT_GROUP)) {
@@ -585,6 +626,22 @@ class AdminCustomersControllerCore extends AdminController
'years' => $years
)
),
array(
'type' => 'select',
'label' => $this->l('Nationality'),
'name' => 'id_country',
'required' => (bool)(Configuration::get('PS_CUSTOMER_NATIONALITY') && Configuration::get('PS_CUSTOMER_NATIONALITY_MANDATORY')),
'col' => '4',
'options' => array(
'query' => Customer::getNationalities($this->context->language->id),
'id' => 'id_country',
'name' => 'name',
'default' => array(
'value' => '',
'label' => $this->l('-- Choose --')
)
),
),
array(
'type' => 'switch',
'label' => $this->l('Enabled'),
@@ -942,6 +999,16 @@ class AdminCustomersControllerCore extends AdminController
$gender = new Gender($customer->id_gender, $this->context->language->id);
$gender_image = $gender->getImage();
$nationality = '';
if ($customer->id_country == -1) {
$nationality = Translate::getAdminTranslation('Other', 'AdminCustomers');
} elseif ($customer->id_country) {
$nationalityCountry = new Country($customer->id_country, $this->context->language->id);
if (Validate::isLoadedObject($nationalityCountry)) {
$nationality = $nationalityCountry->name;
}
}
$customer_stats = $customer->getStats();
$sql = 'SELECT SUM(total_paid_real) FROM '._DB_PREFIX_.'orders WHERE id_customer = %d AND valid = 1';
if ($total_customer = Db::getInstance()->getValue(sprintf($sql, $customer->id))) {
@@ -1091,6 +1158,7 @@ class AdminCustomersControllerCore extends AdminController
'shop_is_feature_active' => Shop::isFeatureActive(),
'name_shop' => $shop->name,
'customer_birthday' => Tools::displayDate($customer->birthday),
'nationality' => $nationality,
'last_update' => Tools::displayDate($customer->date_upd, null, true),
'customer_exists' => Customer::customerExists($customer->email),
'id_lang' => $customer->id_lang,
@@ -1326,6 +1394,10 @@ class AdminCustomersControllerCore extends AdminController
}
}
if (Configuration::get('PS_CUSTOMER_NATIONALITY') && Configuration::get('PS_CUSTOMER_NATIONALITY_MANDATORY') && !Tools::getValue('id_country')) {
$this->errors[] = Tools::displayError('Nationality is required.');
}
$customer = new Customer();
$this->errors = array_merge($this->errors, $customer->validateFieldsRequiredDatabase());

View File

@@ -106,6 +106,9 @@ class AuthControllerCore extends FrontController
$newsletter = Configuration::get('PS_CUSTOMER_NWSL') || (Module::isInstalled('blocknewsletter') && Module::getInstanceByName('blocknewsletter')->active);
$this->context->smarty->assign('birthday', (bool) Configuration::get('PS_CUSTOMER_BIRTHDATE'));
$this->context->smarty->assign('nationality', (bool) Configuration::get('PS_CUSTOMER_NATIONALITY'));
$this->context->smarty->assign('nationality_mandatory', (bool) Configuration::get('PS_CUSTOMER_NATIONALITY_MANDATORY'));
$this->context->smarty->assign('nationality_countries', Customer::getNationalities($this->context->language->id));
$this->context->smarty->assign('newsletter', $newsletter);
$this->context->smarty->assign('optin', (bool)Configuration::get('PS_CUSTOMER_OPTIN'));
@@ -429,6 +432,10 @@ class AuthControllerCore extends FrontController
$this->errors[] = Tools::displayError('Phone number is required.');
}
if (Configuration::get('PS_CUSTOMER_NATIONALITY') && Configuration::get('PS_CUSTOMER_NATIONALITY_MANDATORY') && !Tools::getValue('id_nationality')) {
$this->errors[] = Tools::displayError('Nationality is required.');
}
if (!Tools::getValue('is_new_customer', 1)) {
if (Validate::isEmail($email = Tools::getValue('email'))
&& $idCustomer = Customer::customerExists(Tools::getValue('email'), true, false)
@@ -461,6 +468,7 @@ class AuthControllerCore extends FrontController
if (!count($this->errors)) {
$customer->phone = Tools::getValue('phone');
$customer->id_country = (int)Tools::getValue('id_nationality');
if ($customer->add()) {
if (!$customer->is_guest) {
if (!$this->sendConfirmationMail($customer)) {
@@ -607,6 +615,7 @@ class AuthControllerCore extends FrontController
$customer->is_guest = 0;
}
$customer->phone = Tools::getValue('phone');
$customer->id_country = (int)Tools::getValue('id_nationality');
if (!$customer->save()) {
$this->errors[] = Tools::displayError('An error occurred while creating your account.');
} else {

View File

@@ -63,6 +63,10 @@ class IdentityControllerCore extends FrontController
}
}
if (Configuration::get('PS_CUSTOMER_NATIONALITY') && Configuration::get('PS_CUSTOMER_NATIONALITY_MANDATORY') && !Tools::getValue('id_nationality')) {
$this->errors[] = Tools::displayError('Nationality is required.');
}
if (Tools::getIsset('old_passwd')) {
$old_passwd = trim(Tools::getValue('old_passwd'));
}
@@ -91,6 +95,7 @@ class IdentityControllerCore extends FrontController
if (!count($this->errors)) {
$this->customer->phone = $phone;
$this->customer->id_country = (int)Tools::getValue('id_nationality');
$this->customer->id_default_group = (int)$prev_id_default_group;
$this->customer->firstname = Tools::ucwords($this->customer->firstname);
@@ -127,6 +132,7 @@ class IdentityControllerCore extends FrontController
} else {
$_POST = array_map('stripslashes', $this->customer->getFields());
$_POST['phone'] = $this->customer->phone;
$_POST['id_nationality'] = $this->customer->id_country;
}
return $this->customer;
@@ -167,6 +173,9 @@ class IdentityControllerCore extends FrontController
$newsletter = Configuration::get('PS_CUSTOMER_NWSL') || (Module::isInstalled('blocknewsletter') && Module::getInstanceByName('blocknewsletter')->active);
$this->context->smarty->assign('birthday', (bool) Configuration::get('PS_CUSTOMER_BIRTHDATE'));
$this->context->smarty->assign('nationality', (bool) Configuration::get('PS_CUSTOMER_NATIONALITY'));
$this->context->smarty->assign('nationality_mandatory', (bool) Configuration::get('PS_CUSTOMER_NATIONALITY_MANDATORY'));
$this->context->smarty->assign('nationality_countries', Customer::getNationalities($this->context->language->id));
$this->context->smarty->assign('newsletter', $newsletter);
$this->context->smarty->assign('optin', (bool)Configuration::get('PS_CUSTOMER_OPTIN'));

View File

@@ -126,6 +126,7 @@ class OrderOpcControllerCore extends ParentOrderController
}
$this->context->customer->phone = $phone;
$this->context->customer->id_country = (int)Tools::getValue('id_nationality');
$_POST['lastname'] = $_POST['customer_lastname'];
$_POST['firstname'] = $_POST['customer_firstname'];
$this->errors = array_merge($this->errors, $this->context->customer->validateController());
@@ -549,6 +550,9 @@ class OrderOpcControllerCore extends ParentOrderController
$newsletter = Module::isInstalled('blocknewsletter') && Module::getInstanceByName('blocknewsletter')->active && Configuration::get('PS_CUSTOMER_NWSL');
$this->context->smarty->assign('birthday', (bool) Configuration::get('PS_CUSTOMER_BIRTHDATE'));
$this->context->smarty->assign('nationality', (bool) Configuration::get('PS_CUSTOMER_NATIONALITY'));
$this->context->smarty->assign('nationality_mandatory', (bool) Configuration::get('PS_CUSTOMER_NATIONALITY_MANDATORY'));
$this->context->smarty->assign('nationality_countries', Customer::getNationalities($this->context->language->id));
$this->context->smarty->assign('newsletter', $newsletter);
$this->context->smarty->assign('optin', (bool)Configuration::get('PS_CUSTOMER_OPTIN'));
$this->context->smarty->assign('field_required', $this->context->customer->validateFieldsRequiredDatabase());
@@ -635,6 +639,7 @@ class OrderOpcControllerCore extends ParentOrderController
'id_country' => (int)$address_delivery->id_country,
'id_state' => (int)$address_delivery->id_state,
'id_gender' => (int)$customer->id_gender,
'id_nationality' => (int)$customer->id_country,
'phone' => $customer->phone,
'sl_year' => $birthday[0],
'sl_month' => $birthday[1],

View File

@@ -594,6 +594,7 @@ CREATE TABLE `PREFIX_customer` (
`id_gender` int(10) unsigned NOT NULL,
`id_default_group` int(10) unsigned NOT NULL DEFAULT '1',
`id_lang` int(10) unsigned NULL,
`id_country` int(10) NOT NULL DEFAULT '0',
`id_risk` int(10) unsigned NOT NULL DEFAULT '1',
`company` varchar(64),
`siret` varchar(14),

View File

@@ -911,6 +911,12 @@
<configuration id="PS_CUSTOMER_BIRTHDATE" name="PS_CUSTOMER_BIRTHDATE">
<value>1</value>
</configuration>
<configuration id="PS_CUSTOMER_NATIONALITY" name="PS_CUSTOMER_NATIONALITY">
<value>0</value>
</configuration>
<configuration id="PS_CUSTOMER_NATIONALITY_MANDATORY" name="PS_CUSTOMER_NATIONALITY_MANDATORY">
<value>0</value>
</configuration>
<configuration id="PS_CUSTOMER_NWSL" name="PS_CUSTOMER_NWSL">
<value>1</value>
</configuration>

View File

@@ -504,6 +504,14 @@ function changeCMSActivationAuthorization()
getE('PS_CONDITIONS_CMS_ID').disabled = 'disabled';
}
function toggleNationalityMandatoryField()
{
if ($('#PS_CUSTOMER_NATIONALITY_on').prop('checked'))
$('#conf_id_PS_CUSTOMER_NATIONALITY_MANDATORY').closest('.form-group').show();
else
$('#conf_id_PS_CUSTOMER_NATIONALITY_MANDATORY').closest('.form-group').hide();
}
function changeOverbookingOrderAction()
{
if (getE('PS_OVERBOOKING_ORDER_ACTION').value == $("input[name='OVERBOOKING_ORDER_CANCEL_ACTION']").val())
@@ -998,6 +1006,10 @@ $(document).ready(function()
if ($('.kpi-container').length) {
refresh_kpis();
}
if ($('#PS_CUSTOMER_NATIONALITY_on').length) {
toggleNationalityMandatoryField();
$('input[name="PS_CUSTOMER_NATIONALITY"]').on('change', toggleNationalityMandatoryField);
}
});
function bindSwapSave()

View File

@@ -156,6 +156,17 @@
<label for="lastname">{l s='Last name'} <sup>*</sup></label>
<input type="text" class="is_required validate form-control" data-validate="isName" id="lastname" name="lastname" value="{if isset($smarty.post.lastname)}{$smarty.post.lastname}{/if}" />
</div>
{if isset($nationality) && $nationality}
<div class="{if isset($nationality_mandatory) && $nationality_mandatory}required {/if}form-group">
<label for="id_nationality">{l s='Nationality'}{if isset($nationality_mandatory) && $nationality_mandatory} <sup>*</sup>{/if}</label>
<select name="id_nationality" id="id_nationality" class="form-control">
<option value="">-</option>
{foreach from=$nationality_countries item=c}
<option value="{$c.id_country}" {if isset($smarty.post.id_nationality) && $smarty.post.id_nationality == $c.id_country} selected="selected"{/if}>{$c.name}</option>
{/foreach}
</select>
</div>
{/if}
<div class="form-group date-select">
<label>{l s='Date of Birth'}</label>
<div class="row">
@@ -497,6 +508,17 @@
<input type="password" class="is_required validate form-control" data-validate="isPasswd" name="passwd" id="passwd" />
<span class="form_info">{l s='(Five characters minimum)'}</span>
</div>
{if isset($nationality) && $nationality}
<div class="{if isset($nationality_mandatory) && $nationality_mandatory}required {/if}form-group">
<label for="id_nationality">{l s='Nationality'}{if isset($nationality_mandatory) && $nationality_mandatory} <sup>*</sup>{/if}</label>
<select name="id_nationality" id="id_nationality" class="form-control">
<option value="">-</option>
{foreach from=$nationality_countries item=c}
<option value="{$c.id_country}" {if isset($smarty.post.id_nationality) && $smarty.post.id_nationality == $c.id_country} selected="selected"{/if}>{$c.name}</option>
{/foreach}
</select>
</div>
{/if}
{if isset($birthday) && $birthday}
<div class="form-group">
<label>{l s='Date of Birth'}</label>

View File

@@ -95,6 +95,19 @@
</label>
<input class="is_required validate form-control" data-validate="isPhoneNumber" type="phone" name="phone" id="phone" value="{$smarty.post.phone}" />
</div>
{if isset($nationality) && $nationality}
<div class="{if isset($nationality_mandatory) && $nationality_mandatory}required {/if}form-group">
<label for="id_nationality"{if isset($nationality_mandatory) && $nationality_mandatory} class="required"{/if}>
{l s='Nationality'}
</label>
<select name="id_nationality" id="id_nationality" class="form-control">
<option value="">-</option>
{foreach from=$nationality_countries item=c}
<option value="{$c.id_country}" {if isset($smarty.post.id_nationality) && $smarty.post.id_nationality == $c.id_country} selected="selected"{/if}>{$c.name}</option>
{/foreach}
</select>
</div>
{/if}
{if isset($birthday) && $birthday}
<div class="form-group">
<label>

View File

@@ -91,6 +91,17 @@
<label for="lastname">{l s='Last name'} <sup>*</sup></label>
<input type="text" class="form-control validate" id="customer_lastname" name="customer_lastname" onblur="$('#lastname').val($(this).val());" data-validate="isName" value="{if isset($guestInformations) && isset($guestInformations.customer_lastname) && $guestInformations.customer_lastname}{$guestInformations.customer_lastname}{/if}" />
</div>
{if isset($nationality) && $nationality}
<div class="{if isset($nationality_mandatory) && $nationality_mandatory}required {/if}form-group">
<label for="id_nationality">{l s='Nationality'}{if isset($nationality_mandatory) && $nationality_mandatory} <sup>*</sup>{/if}</label>
<select name="id_nationality" id="id_nationality" class="form-control">
<option value="">-</option>
{foreach from=$nationality_countries item=c}
<option value="{$c.id_country}"{if isset($smarty.post.id_nationality) && $smarty.post.id_nationality == $c.id_country || (isset($guestInformations) && isset($guestInformations.id_nationality) && $guestInformations.id_nationality == $c.id_country)} selected="selected"{/if}>{$c.name|escape:'html':'UTF-8'}</option>
{/foreach}
</select>
</div>
{/if}
<div class="select form-group date-select">
<label>{l s='Date of Birth'}</label>
<div class="row">

View File

@@ -116,6 +116,19 @@
<input type="text" class="text form-control validate" name="customer_phone" id="customer_phone" data-validate="isPhoneNumber" value="{if isset($guestInformations) && isset($guestInformations.phone_mobile) && $guestInformations.phone_mobile}{$guestInformations.phone_mobile}{/if}" onblur="$('#phone').val($(this).val());"/>
</div>
</div>
{if isset($nationality) && $nationality}
<div class="row">
<div class="{if isset($nationality_mandatory) && $nationality_mandatory}required {/if}form-group col-sm-6">
<label for="id_nationality">{l s='Nationality'}{if isset($nationality_mandatory) && $nationality_mandatory} <sup>*</sup>{/if}</label>
<select name="id_nationality" id="id_nationality" class="form-control">
<option value="">-</option>
{foreach from=$nationality_countries item=c}
<option value="{$c.id_country}"{if isset($smarty.post.id_nationality) && $smarty.post.id_nationality == $c.id_country || (isset($guestInformations) && isset($guestInformations.id_nationality) && $guestInformations.id_nationality == $c.id_country)} selected="selected"{/if}>{$c.name|escape:'html':'UTF-8'}</option>
{/foreach}
</select>
</div>
</div>
{/if}
{if isset($PS_REGISTRATION_PROCESS_TYPE) && $PS_REGISTRATION_PROCESS_TYPE}
{if isset($birthday) && $birthday}
<div class="row">