Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init JS when Magewire is loaded #4

Merged
merged 5 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Model/Form/Modifier/AddHousenumberFieldValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class AddHousenumberFieldValidation implements EntityFormModifierInterface
/**
* Add validation to the housenumber field if street has only one relative, which should mean that there are two
* street fields.
*
* This form modifier is included in the module to make sure that the address the autocomplete API returns contains
* an housenumber, since it's possible that the API returns an address without a housenumber.
* If the housenumber is not present, the address is not valid and the customer cannot continue.
*/
public function apply(EntityFormInterface $form): EntityFormInterface
{
Expand Down
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Coding Standard](https://github.com/Vendic/hyva-checkout-google-address-autocomplete/actions/workflows/coding-standard.yml/badge.svg)](https://github.com/Vendic/hyva-checkout-google-address-autocomplete/actions/workflows/coding-standard.yml)

Hyvä checkout compatibility module for [vendic/magento2-google-address-autocomplete](https://github.com/Vendic/magento2-google-address-autocomplete).
[Hyvä checkout](https://www.hyva.io/hyva-checkout.html) compatibility module for [vendic/magento2-google-address-autocomplete](https://github.com/Vendic/magento2-google-address-autocomplete).



Expand All @@ -16,6 +16,9 @@ composer require vendic/hyva-checkout-google-address-autocomplete
```

## Configuration
First, create your Google Maps API key. You can find instructions on how to do this [here](https://developers.google.com/maps/get-started#create-project).
Also make sure you have valid billing information added to your Google account.

Set your Google Maps API key in the Magento admin panel under `Stores > Configuration > Vendic > Google Address Autocomplete`.

Or - even better - via the CLI:
Expand All @@ -30,5 +33,13 @@ There is no disable/enable configuration. To disable the module, simply remove t
- [x] Autocomplete for shipping address in the Hyvä checkout
- [ ] Autocomplete for adding a new address as a logged in user
- [x] Works on the company field and street 0. To add additonal fields, check `\Vendic\HyvaCheckoutGoogleAddressAutocomplete\ViewModel\AutoCompleteSelectors`
- [x] Housenumber validation to check if the housenumber contains digits.
- [x] Housenumber validation to check if the housenumber contains digits.

## Customizations
### Field mapping
The field mapping (google address response mapped to Hyvä checkout form inputs) can be modified using di.xml.
See `etc/frontend/di.xml` for the default mapping, which you can change edit in [your own di.xml](https://devdocs.mage-os.org/docs/main/di_xml#content-syntax).

### Input selectors
The input selectors (the fields that trigger the autocomplete dropdown ) can be modified using di.xml.
By default, the autocomplete is triggered for street 0 and company.
3 changes: 2 additions & 1 deletion etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
<type name="Vendic\HyvaCheckoutGoogleAddressAutocomplete\ViewModel\FieldMapping">
<arguments>
<argument name="fieldMapping" xsi:type="array">
<item name="company" xsi:type="string">company</item>
<!-- in this case, route is street name key from the API response, it's mapped to the input street.0 -->
<item name="route" xsi:type="string">street.0</item>
<item name="company" xsi:type="string">company</item>
<item name="street_number" xsi:type="string">street.1</item>
<item name="locality" xsi:type="string">city</item>
<item name="postal_code" xsi:type="string">postcode</item>
Expand Down
10 changes: 7 additions & 3 deletions view/frontend/templates/checkout/google-autocomplete-js.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ if (!$apiKey) {

let googleAutoCompleteInputs = [];

// Init autocomplete JS when Alpine is ready
document.addEventListener('alpine:init', () => { initAutoCompleteJS() });
// Initialize autocomplete when Magewire is loaded
document.addEventListener('magewire:load', () => {
initAutoCompleteJS()
}, { once: true });

// Re-init autocomplete when billing as shipping is toggled
window.addEventListener('billing-as-shipping-toggled', () => { reInitAutocomplete()} );

// Wait for Google Maps JS callback to add autocomplete to specified inputs
document.addEventListener('google_maps_js_loaded', () => { addAutocompleteToInputs(); });

// Listen for address changes from Google Autocomplete
window.addEventListener('google-autocomplete-address-changed', (event) => {
console.debug(`google-autocomplete-address-changed for ${event.detail.addressType}`, event.detail);
Expand Down Expand Up @@ -84,7 +89,6 @@ if (!$apiKey) {
const identifier = `address.${fieldMappings[key]}`;

if (value !== undefined && value.length > 0) {
console.debug(`Setting ${identifier} to ${value}`);
component.set(identifier, value);
}
});
Expand Down