Skip to content

Autocomplete

The autocomplete abstraction layer allows you to create form autocomplete functionalities using third-party address providers such as PostcodeNL or Google.

Please refer to Address finders and address autocomplete for all services that are already integrated using this abstraction.

Installation

composer require hyva-themes/magento2-hyva-checkout-autocomplete

Requirements

  • PHP: 7.4 (or higher)

Usage

Build yourself a custom module which has a sequence entry for Hyva_CheckoutAutoComplete.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"
>
    <module name="Vendor_Module">
        <sequence>
            <module name="Hyva_CheckoutAutoComplete"/>
        </sequence>
    </module>
</config>

Create a custom Auto Complete Adapter which extends from \Hyva\CheckoutAutoComplete\Model\AddressAutoCompleteServiceAdapter\AbstractServiceAdapter.

<?php

declare(strict_types=1);

namespace Vendor\Module\Model\AddressAutoCompleteServiceAdapter;

use Hyva\Checkout\Model\Form\AbstractEntityForm;
use Hyva\Checkout\Model\Form\EntityFormModifierInterface;
use Hyva\CheckoutAutoComplete\Model\AddressAutoCompleteServiceAdapter\AbstractServiceAdapter;

class YourServiceAdapter extends AbstractServiceAdapter
{
    private array $entityFormModifiers;

    public function __construct(
        array $entityFormModifiers = []
    ) {
        $this->entityFormModifiers = $entityFormModifiers;
    }

    public function getServiceName(): string
    {
        return 'Your Service Name Here';
    }

    public function accessServiceApi(): object
    {
        // Return an empty object as a placeholder since no API client is needed in this instance.
        // If you're integrating with an existing PHP API client from the module you're writing a compatibility module for, 
        // replace this with the appropriate API client object.
        return (object) [];
    }

    public function canApplyEntityFormModifications(): bool
    {
        // You can add a condition here to determine if the autocomplete form modifications should be applied.
        // For example: checking if valid credentials for the service have been set in configuration.
    }

    public function modifyEntityForm(AbstractEntityForm $form): void
    {
        /** @var EntityFormModifierInterface $modifier */
        foreach ($this->entityFormModifiers as $modifier) {
            $modifier->apply($form);
        }
    }
}

Inject it as one of the available adapters via etc/di.xml.

<type name="Hyva\CheckoutAutoComplete\Model\AddressAutoCompleteServiceAdapterProvider">
    <arguments>
        <argument name="adapters" xsi:type="array">
            <item name="your_service_name" xsi:type="object">
                \Vendor\Module\Model\AddressAutoCompleteServiceAdapter\YourServiceAdapter
            </item>
        </argument>
    </arguments>
</type>