Skip to content

Commit

Permalink
Merge pull request #51 from JerrySmidt/3.1.6
Browse files Browse the repository at this point in the history
3.1.6 Validate autocomplete field
  • Loading branch information
JozefVerhoef authored Jul 4, 2022
2 parents c68d748 + 50633f2 commit c381a0b
Show file tree
Hide file tree
Showing 20 changed files with 634 additions and 172 deletions.
10 changes: 5 additions & 5 deletions Api/Data/Autocomplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Autocomplete implements AutocompleteInterface
/**
* @var Autocomplete\MatchInterface[]
*/
public $matches = [];
public $matches = [];

/**
* __construct function.
Expand All @@ -16,13 +16,13 @@ class Autocomplete implements AutocompleteInterface
* @param array $response
* @return void
*/
public function __construct(array $response)
{
foreach ($response['matches'] as $match)
public function __construct(array $response)
{
foreach ($response['matches'] ?? [] as $match)
{
$this->matches[] = new Autocomplete\AutocompleteMatch($match);
}
}
}

/**
* @inheritdoc
Expand Down
161 changes: 91 additions & 70 deletions Block/Onepage/LayoutProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class LayoutProcessor extends AbstractBlock implements LayoutProcessorInterface
{
protected $scopeConfig;


/**
* __construct function.
*
Expand All @@ -26,7 +25,6 @@ public function __construct(Context $context, array $data = [])
parent::__construct($context, $data);
}


/**
* process function.
*
Expand All @@ -38,103 +36,126 @@ public function process($jsLayout)
{
$moduleEnabled = $this->scopeConfig->getValue('postcodenl_api/general/enabled', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

if ($moduleEnabled && isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset'])) {
$formFields = $this->_getFormFields($jsLayout['components']['checkout']['children']['steps']['children']);
if (!$moduleEnabled) {
return $jsLayout;
}

foreach ($formFields as &$fields) {
$fields = array_merge($fields, $this->_getAutofillFields($jsLayout));
$fields = $this->changeAddressFieldPosition($fields);
// Shipping fields
$shippingFields = &$jsLayout['components']
['checkout']['children']
['steps']['children']
['shipping-step']['children']
['shippingAddress']['children']
['shipping-address-fieldset']['children'];

$shippingFields = $this->_changeAddressFieldsPosition($shippingFields);

// Autofill fields copy
$autofillFields = array_intersect_key($shippingFields, ['address_autofill_nl' => 1, 'address_autofill_intl' => 1, 'address_autofill_formatted_output' => 1]);

// Billing step
$billingConfiguration = &$jsLayout['components']
['checkout']['children']
['steps']['children']
['billing-step']['children']
['payment']['children']
['payments-list']['children'];

if (isset($billingConfiguration)) {
foreach($billingConfiguration as $key => &$billingForm) {
if (!strpos($key, '-form')) {
continue;
}

// Billing fields
$billingForm['children']['form-fields']['children'] += $this->_updateCustomScope($autofillFields, $billingForm['dataScopePrefix']);
$billingForm['children']['form-fields']['children'] = $this->_changeAddressFieldsPosition($billingForm['children']['form-fields']['children']);
}
}

// Billing address on payment page
$billingFields = &$jsLayout['components']
['checkout']['children']
['steps']['children']
['billing-step']['children']
['payment']['children']
['afterMethods']['children']
['billing-address-form']['children']
['form-fields']['children'];

if (isset($billingFields)) {
$billingFields += $this->_updateCustomScope($autofillFields, 'billingAddressshared');
$billingFields = $this->_changeAddressFieldsPosition($billingFields);
}

// Compatibility
$magePlazaBillingFields = &$jsLayout['components']
['checkout']['children']
['steps']['children']
['shipping-step']['children']
['billingAddress']['children']
['billing-address-fieldset']['children'];

if (isset($magePlazaBillingFields)) {
$magePlazaBillingFields += $this->_updateCustomScope($autofillFields, 'billingAddress');
$magePlazaBillingFields = $this->_changeAddressFieldsPosition($magePlazaBillingFields);
}

return $jsLayout;
}

/**
* Get references to $jsLayout form fields.
* Find and update customScope
*
* @access private
* @param mixed $jsLayout
* @param array $result - Accumulates form fields.
* @return array - Array of form fields by reference.
* @param array $fields
* @param string $dataScope
* @return array - Fields with modified customScope.
*/
private function _getFormFields(&$jsLayout, &$result = [])
private function _updateCustomScope($fields, $dataScope)
{
foreach ($jsLayout as $name => &$value) {
if (in_array($name, ['form-fields', 'shipping-address-fieldset', 'billing-address-fieldset'], true)) {
$result[] = &$value['children'];
foreach ($fields as $name => $items) {
if (isset($items['config'], $items['config']['customScope'])) {
$fields[$name]['config']['customScope'] = $dataScope;
}
else if (is_array($value)) {
$this->_getFormFields($value, $result);

if (isset($items['children'])) {
$fields[$name]['children'] = $this->_updateCustomScope($items['children'], $dataScope);
}
}

return $result;
return $fields;
}

/**
* Get autofill fields from shipping fieldset.
* Change sort order of address fields.
*
* @access private
* @param mixed $jsLayout
* @return array
*/
private function _getAutofillFields($jsLayout)
{
$shippingFields = $jsLayout['components']['checkout']['children']['steps']['children']
['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children'];

return array_intersect_key($shippingFields, ['address_autofill_nl' => 1, 'address_autofill_intl' => 1, 'address_autofill_formatted_output' => 1]);
}

/**
* changeAddressFieldPosition function.
*
* @access public
* @param mixed $addressFields
* @param array $addressFields
* @return array
*/
public function changeAddressFieldPosition($addressFields)
private function _changeAddressFieldsPosition($addressFields)
{
if ($this->scopeConfig->getValue('postcodenl_api/general/change_fields_position') != '1') {
return $addressFields;
}

if (isset($addressFields['country_id'])) {
$addressFields['country_id']['sortOrder'] = '900';
}

if (isset($addressFields['address_autofill_intl'])) {
$addressFields['address_autofill_intl']['sortOrder'] = '910';
}

if (isset($addressFields['address_autofill_nl'])) {
$addressFields['address_autofill_nl']['sortOrder'] = '920';
}

if (isset($addressFields['address_autofill_formatted_output'])) {
$addressFields['address_autofill_formatted_output']['sortOrder'] = '930';
}

if (isset($addressFields['street'])) {
$addressFields['street']['sortOrder'] = '940';
}

if (isset($addressFields['postcode'])) {
$addressFields['postcode']['sortOrder'] = '950';
}

if (isset($addressFields['city'])) {
$addressFields['city']['sortOrder'] = '960';
}

if (isset($addressFields['region'])) {
$addressFields['region']['sortOrder'] = '970';
}

if (isset($addressFields['region_id'])) {
$addressFields['region_id']['sortOrder'] = '975';
$fieldToSortOrder = [
'country_id' => '900',
'address_autofill_intl' => '910',
'address_autofill_nl' => '920',
'address_autofill_formatted_output' => '930',
'street' => '940',
'postcode' => '950',
'city' => '960',
'region' => '970',
'region_id' => '975',
];

foreach ($fieldToSortOrder as $name => $sortOrder) {
if (isset($addressFields[$name])) {
$addressFields[$name]['sortOrder'] = $sortOrder;
}
}

return $addressFields;
Expand Down
5 changes: 4 additions & 1 deletion Helper/ApiClientHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ public function getNlAddress(string $zipCode, string $houseNumber): array
$address = $this->_prepareResponse($address, $client);
$status = 'valid';

if (!is_null($houseNumberAddition) && (is_null($address['houseNumberAddition']) || strcasecmp($houseNumberAddition, $address['houseNumberAddition']) != 0)
if (
(strcasecmp($address['houseNumberAddition'] ?? '', $houseNumberAddition ?? '') != 0)
||
(!empty($address['houseNumberAdditions']) && is_null($address['houseNumberAddition']))
) {
$status = 'houseNumberAdditionIncorrect';
}
Expand Down
2 changes: 1 addition & 1 deletion etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<type name="Magento\Checkout\Block\Onepage">
<arguments>
<argument name="layoutProcessors" xsi:type="array">
<item name="flekto_postcode_layoutprocessor" xsi:type="object">Flekto\Postcode\Block\Onepage\LayoutProcessor</item>
<item name="flekto_postcode_layoutprocessor" xsi:type="object" sortOrder="1">Flekto\Postcode\Block\Onepage\LayoutProcessor</item>
</argument>
</arguments>
</type>
Expand Down
3 changes: 3 additions & 0 deletions i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@
"Address not found.","Address not found."
"Please enter a valid zip/postal code.","Please enter a valid zip/postal code."
"Please enter a valid house number.","Please enter a valid house number."
"Please enter an address and select it.","Please enter an address and select it."
"An error has occurred while retrieving address data. Please contact us if the problem persists.","An error has occurred while retrieving address data. Please contact us if the problem persists."
"Please select a house number.","Please select a house number."
3 changes: 3 additions & 0 deletions i18n/nl_NL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@
"Address not found.","Adres niet gevonden."
"Please enter a valid zip/postal code.","Vul alstublieft een geldige postcode in."
"Please enter a valid house number.","Vul alstublieft een geldig huisnummer in."
"Please enter an address and select it.","Vul alstublieft een adres in en selecteer het."
"An error has occurred while retrieving address data. Please contact us if the problem persists.","Er is een fout opgetreden bij het ophalen van adres gegevens. Neem contact met ons op als het probleem blijft optreden."
"Please select a house number.","Selecteer alstublieft een huisnummer."
Loading

0 comments on commit c381a0b

Please sign in to comment.