PHP code example of addressfinder / module-magento2
1. Go to this page and download the library: Download addressfinder/module-magento2 library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
addressfinder / module-magento2 example snippets
use \Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Acme_CustomForm', __DIR__);
namespace Acme\CustomForm\Observer\FormConfig\Frontend;
use AddressFinder\AddressFinder\Observer\FormConfig\Base;
use Exception;
use Magento\Framework\Data\Collection;
use Magento\Framework\DataObject;
class AddStoreLocator extends Base
{
const FORM_ID = 'frontend.store.locator';
/**
* @inheritDoc
*
* @throws Exception
*/
protected function addForm(Collection $forms): void
{
$forms->addItem(new DataObject([
// A unique form ID to identify this form within the AddressFinder module
'id' => self::FORM_ID,
// A semantic label
'label' => 'Store Locatork',
// The selector for where to instantiate the JavaScript widget
'layoutSelectors' => ['input#street_1'],
// The country selector that switches the form between AU and NZ
'countryIdentifier' => 'select[name=country_id]',
// The search box selector - this is where your users type to trigger the AddressFinder autocomplete
'searchIdentifier' => 'input#street_1',
// NZ-specific config
'nz' => [
// The value which the `countryIdentifier` is set as to represent NZ
'countryValue' => 'NZ',
// Varying element selectors to place the selected address result in
'elements' => [
'address1' => 'input#street_1',
'suburb' => 'input#street_2',
'city' => 'input[name=city]',
'region' => 'input[name=region]',
'postcode' => 'input[name=postcode]',
],
'regionMappings' => null,
],
// AU-specific config
'au' => [
// The value which the `countryIdentifier` is set as to represent AU
'countryValue' => 'AU',
// Varying element selectors to place the selected address result in
'elements' => [
'address1' => 'input#street_1',
'address2' => 'input#street_2',
'suburb' => 'input[name=city]',
// This helper from the base class we extend will allow us to
// support free-form state inputs as well as dropdown menus.
'state' => $this->getStateMappings('AU')
? 'select[name=region_id]'
: 'input[name=region]',
'postcode' => 'input[name=postcode]',
],
// State mappings for Australia (if they exist in your Magento installation)
'stateMappings' => $this->getStateMappings('AU'),
],
]));
}
}
namespace Acme\CustomForm\Observer\Config\Source\Frontend;
use Acme\CustomForm\Observer\FormConfig\Frontend\AddStoreLocator;
use Exception;
use Magento\Framework\Data\Collection;
use Magento\Framework\DataObject;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class StoreLocator implements ObserverInterface
{
/**
* @inheritDoc
*
* @throws Exception
*/
public function execute(Observer $observer): void
{
/** @var Collection $frontend */
// If you were building an admin form, you'd call `getData('admin')` and append the form to that list
$frontend = $observer->getEvent()->getData('frontend');
$frontend->addItem(new DataObject([
'label' => 'Store Locator',
'value' => AddStoreLocator::FORM_ID,
]));
}
}