Prestashop 1.7 e la gestione del codice fiscale obbligatorio solo per l’Italia.
La guida ufficiale, propone di andare su Località, Italia, abilitare il campo codice fiscale, in seguito andare su clienti, indirizzi ed aggiungere il codice fiscale come campo obbligatorio.
Ero felice, tutto funzionava in maniera egregia.
Il problema è quando mi sono registrato con l’indirizzo non italiano ma Francese, al momento di confermare l’indirizzo compare un clamoroso Errore 500.
Il problema (bug) è che il codice fiscale risulta obbligatorio anche per gli altri paesi, ma non compare ovviamente nei campi disponibili in quanto non abilitato.
Ho girovagato per forum per trovare una soluzione…che ho trovato :-)))) e oggi voglio condividerla con chiunque ne abbia bisogno.
Vi dico subito che il problema sarà risolto nella versione 1.7.3 di Prestashop, ma per ora mi sembra una buona soluzione.
Ecco cosa fare
si tratta di aggiungere nel file: classes/form/CustomerAddressFormatter.php questo codice intorno alla riga 100.
if ($field === ‘dni‘) { | |
if ($this->country->need_identification_number) | |
{ $formField->setRequired(true); } | |
} | |
} |
Il file completo dovrebbe essere così:
<?php | |
/** | |
* 2007-2018 PrestaShop | |
* | |
* NOTICE OF LICENSE | |
* | |
* This source file is subject to the Open Software License (OSL 3.0) | |
* that is bundled with this package in the file LICENSE.txt. | |
* It is also available through the world-wide-web at this URL: | |
* https://opensource.org/licenses/OSL-3.0 | |
* If you did not receive a copy of the license and are unable to | |
* obtain it through the world-wide-web, please send an email | |
* to [email protected] so we can send you a copy immediately. | |
* | |
* DISCLAIMER | |
* | |
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer | |
* versions in the future. If you wish to customize PrestaShop for your | |
* needs please refer to http://www.prestashop.com for more information. | |
* | |
* @author PrestaShop SA <[email protected]> | |
* @copyright 2007-2018 PrestaShop SA | |
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) | |
* International Registered Trademark & Property of PrestaShop SA | |
*/ | |
use Symfony\Component\Translation\TranslatorInterface; | |
class CustomerAddressFormatterCore implements FormFormatterInterface | |
{ | |
private $country; | |
private $translator; | |
private $availableCountries; | |
private $definition; | |
public function __construct( | |
Country $country, | |
TranslatorInterface $translator, | |
array $availableCountries | |
) { | |
$this->country = $country; | |
$this->translator = $translator; | |
$this->availableCountries = $availableCountries; | |
$this->definition = Address::$definition[‘fields‘]; | |
} | |
public function setCountry(Country $country) | |
{ | |
$this->country = $country; | |
return $this; | |
} | |
public function getCountry() | |
{ | |
return $this->country; | |
} | |
public function getFormat() | |
{ | |
$fields = AddressFormat::getOrderedAddressFields( | |
$this->country->id, | |
true, | |
true | |
); | |
$required = array_flip(AddressFormat::getFieldsRequired()); | |
$format = [ | |
‘id_address‘ => (new FormField) | |
->setName(‘id_address‘) | |
->setType(‘hidden‘), | |
‘id_customer‘ => (new FormField) | |
->setName(‘id_customer‘) | |
->setType(‘hidden‘), | |
‘back‘ => (new FormField) | |
->setName(‘back‘) | |
->setType(‘hidden‘), | |
‘token‘ => (new FormField) | |
->setName(‘token‘) | |
->setType(‘hidden‘), | |
‘alias‘ => (new FormField) | |
->setName(‘alias‘) | |
->setLabel( | |
$this->getFieldLabel(‘alias‘) | |
) | |
]; | |
foreach ($fields as $field) { | |
$formField = new FormField(); | |
$formField->setName($field); | |
$fieldParts = explode(‘:‘, $field, 2); | |
if (count($fieldParts) === 1) { | |
if ($field === ‘postcode‘) { | |
if ($this->country->need_zip_code) { | |
$formField->setRequired(true); | |
} | |
} | |
// for countries where DNI is mandatory | |
// but dni is unabled in Customers -> Address | |
// and enabled in Countries -> each country where DNI is not optional | |
if ($field === ‘dni‘) { | |
if ($this->country->need_identification_number) | |
{ $formField->setRequired(true); } | |
} | |
} elseif (count($fieldParts) === 2) { | |
list($entity, $entityField) = $fieldParts; | |
// Fields specified using the Entity:field | |
// notation are actually references to other | |
// entities, so they should be displayed as a select | |
$formField->setType(‘select‘); | |
// Also, what we really want is the id of the linked entity | |
$formField->setName(‘id_‘.strtolower($entity)); | |
if ($entity === ‘Country‘) { | |
$formField->setType(‘countrySelect‘); | |
$formField->setValue($this->country->id); | |
foreach ($this->availableCountries as $country) { | |
$formField->addAvailableValue( | |
$country[‘id_country‘], | |
$country[$entityField] | |
); | |
} | |
} elseif ($entity === ‘State‘) { | |
if ($this->country->contains_states) { | |
$states = State::getStatesByIdCountry($this->country->id); | |
foreach ($states as $state) { | |
$formField->addAvailableValue( | |
$state[‘id_state‘], | |
$state[$entityField] | |
); | |
} | |
$formField->setRequired(true); | |
} | |
} | |
} | |
$formField->setLabel($this->getFieldLabel($field)); | |
if (!$formField->isRequired()) { | |
// Only trust the $required array for fields | |
// that are not marked as required. | |
// $required doesn’t have all the info, and fields | |
// may be required for other reasons than what | |
// AddressFormat::getFieldsRequired() says. | |
$formField->setRequired( | |
array_key_exists($field, $required) | |
); | |
} | |
$format[$formField->getName()] = $formField; | |
} | |
return $this->addConstraints( | |
$this->addMaxLength( | |
$format | |
) | |
); | |
} | |
private function addConstraints(array $format) | |
{ | |
foreach ($format as $field) { | |
if (!empty($this->definition[$field->getName()][‘validate‘])) { | |
$field->addConstraint( | |
$this->definition[$field->getName()][‘validate‘] | |
); | |
} | |
} | |
return $format; | |
} | |
private function addMaxLength(array $format) | |
{ | |
foreach ($format as $field) { | |
if (!empty($this->definition[$field->getName()][‘size‘])) { | |
$field->setMaxLength( | |
$this->definition[$field->getName()][‘size‘] | |
); | |
} | |
} | |
return $format; | |
} | |
private function getFieldLabel($field) | |
{ | |
// Country:name => Country, Country:iso_code => Country, | |
// same label regardless of which field is used for mapping. | |
$field = explode(‘:‘, $field)[0]; | |
switch ($field) { | |
case ‘alias‘: | |
return $this->translator->trans(‘Alias‘, [], ‘Shop.Forms.Labels‘); | |
case ‘firstname‘: | |
return $this->translator->trans(‘First name‘, [], ‘Shop.Forms.Labels‘); | |
case ‘lastname‘: | |
return $this->translator->trans(‘Last name‘, [], ‘Shop.Forms.Labels‘); | |
case ‘address1‘: | |
return $this->translator->trans(‘Address‘, [], ‘Shop.Forms.Labels‘); | |
case ‘address2‘: | |
return $this->translator->trans(‘Address Complement‘, [], ‘Shop.Forms.Labels‘); | |
case ‘postcode‘: | |
return $this->translator->trans(‘Zip/Postal Code‘, [], ‘Shop.Forms.Labels‘); | |
case ‘city‘: | |
return $this->translator->trans(‘City‘, [], ‘Shop.Forms.Labels‘); | |
case ‘Country‘: | |
return $this->translator->trans(‘Country‘, [], ‘Shop.Forms.Labels‘); | |
case ‘State‘: | |
return $this->translator->trans(‘State‘, [], ‘Shop.Forms.Labels‘); | |
case ‘phone‘: | |
return $this->translator->trans(‘Phone‘, [], ‘Shop.Forms.Labels‘); | |
case ‘phone_mobile‘: | |
return $this->translator->trans(‘Mobile phone‘, [], ‘Shop.Forms.Labels‘); | |
case ‘company‘: | |
return $this->translator->trans(‘Company‘, [], ‘Shop.Forms.Labels‘); | |
case ‘vat_number‘: | |
return $this->translator->trans(‘VAT number‘, [], ‘Shop.Forms.Labels‘); | |
case ‘dni‘: | |
return $this->translator->trans(‘Identification number‘, [], ‘Shop.Forms.Labels‘); | |
case ‘other‘: | |
return $this->translator->trans(‘Other‘, [], ‘Shop.Forms.Labels‘); | |
default: | |
return $field; | |
} | |
} | |
} |
Spero che questo articolo ti sia stato utile.