定制 dravasp/addressautocomplete 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dravasp/addressautocomplete

Composer 安装命令:

composer require dravasp/addressautocomplete

包简介

Google Places API Address Autocomplete - Magento 2 Address Autocomplete Module using Google Places API

README 文档

README

Magento OS Commerce 2.4.8 (ver.2.4x) compatible module tha handles Address Autocomplete on Customer Registration, Billing and Shipping Address Line on Magento Hosted Checkout Page - uses the Google Places API - Compliant AVS (Address Verification System) Amex Discover Mastercard Visa - Google Enterprise API - Google Maps - Google Plus Codes - Google Maps Javascript API (Legacy) compliant

cd /opt/bitnami/magento
composer require dravasp/addressautocomplete:dev-master
sudo magento-cli setup:upgrade

code Data Structure - Magento Magento module will handle Address Autocomplete using the Google Places API This guide provides a step-by-step approach to replicate the address autocomplete functionality without using any specific third-party APIs or components.

Directory Structure is as follows -

app/code/Magento/AddressAutoComplete/
├── registration.php
├── etc
│   ├── config.xml
│   ├── csp_whitelist.xml
│   └── di.xml
│   └── module.xml
├── Helper
│   └── Data.php
├── Model
│   └── AutoCompleteConfigProvider.php
├── ViewModel
│   └── Autocomplete.php
├── view
│   └── frontend
│       └── layout
│           └── checkout_index_index.xml
│       └── web
│           └── js
│               ├── autocomplete.js
│               └── google_maps_loader.js
├── templates
│   └── address
│       └── autocomplete.phtml
├── composer.json
└── .gitignore

View Postman API Collection Workspace - https://bit.ly/3To4cur

1. Registration File `registration.php`
File: app/code/Magento/AddressAutoComplete/registration.php
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_AddressAutoComplete', __DIR__);
2. Module Declaration File `module.xml` **********
File: app/code/Magento/AddressAutoComplete/etc/module.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Magento_AddressAutoComplete" setup_version="0.0.1"/>
</config>
3. Dependency Injection Configuration (Optional) **********
If you need to configure dependency injection, you can create a di.xml file.
File: app/code/Magento/AddressAutoComplete/etc/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Model\CompositeConfigProvider">
        <arguments>
            <argument name="configProviders" xsi:type="array">
                <item name="address_autocomplete" xsi:type="object">Magento\AddressAutoComplete\Model\AutoCompleteConfigProvider</item>
            </argument>
        </arguments>
    </type>
</config>
4. Create `config.xml`
File: app/code/Magento/AddressAutoComplete/etc/config.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/system_file.xsd">
    <system>
        <section id="shipping">
            <group id="address_autocomplete" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Address Autocomplete Settings</label>
                <field id="active" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enabled</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="google_api_key" translate="label" sortOrder="120" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Google API Key</label>
                </field>
                <field id="use_geolocation" type="select" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Use Geolocation</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="use_long_postcode" type="select" translate="label" sortOrder="140" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Use Long Postcode</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
            </group>
        </section>
    </system>
</config>
5. Helper Class
File: app/code/Magento/AddressAutoComplete/Helper/Data.php
<?php
namespace Magento\AddressAutoComplete\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
class Data extends AbstractHelper
{
    public function getConfigValue($field)
    {
        return $this->scopeConfig->getValue($field, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    }
}
6. Config Provider
File: app/code/Magento/AddressAutoComplete/Model/AutoCompleteConfigProvider.php
<?php
namespace Magento\AddressAutoComplete\Model;

use Magento\Checkout\Model\ConfigProviderInterface;

class AutoCompleteConfigProvider implements ConfigProviderInterface
{
    private $helper;

    public function __construct(\Magento\AddressAutoComplete\Helper\Data $helper)
    {
        $this->helper = $helper;
    }

    public function getConfig()
    {
        return [
            'address_autocomplete' => [
                'active' => $this->helper->getConfigValue('shipping/address_autocomplete/active'),
                'api_key' => $this->helper->getConfigValue('shipping/address_autocomplete/google_api_key'),
                'use_geolocation' => $this->helper->getConfigValue('shipping/address_autocomplete/use_geolocation'),
                'use_long_postcode' => $this->helper->getConfigValue('shipping/address_autocomplete/use_long_postcode')
            ]
        ];
    }
}
7. Create `Autocomplete.php`
File: app/code/Magento/AddressAutoComplete/ViewModel/Autocomplete.php
<?php
namespace Magento\AddressAutoComplete\ViewModel;

use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;

class Autocomplete implements ArgumentInterface
{
    protected $_scopeConfig;

    public function __construct(ScopeConfigInterface $scopeConfig) {
        $this->_scopeConfig = $scopeConfig;
    }

    public function getConfig($path)
    {
        return $this->_scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    }
}
8. Layout XML File - checkout_index_index.xml
File: app/code/Magento/AddressAutoComplete/view/frontend/layout/checkout_index_index.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="checkout" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.root">
            <arguments>
                <argument name="jsLayout" xsi:type="array">
                    <item name="components" xsi:type="array">
                        <item name="checkout" xsi:type="array">
                            <item name="children" xsi:type="array">
                                <item name="autocomplete" xsi:type="array">
                                    <item name="component" xsi:type="string">Magento_AddressAutoComplete/js/autocomplete</item>
                                </item>
                            </item>
                        </item>
                    </item>
                </argument>
            </arguments>
        </referenceBlock>
    </body>
</page>
9. JavaScript File
File: app/code/Magento/AddressAutoComplete/view/frontend/web/js/autocomplete.js
define(['jquery'], function ($) {
    var apiKey = window.checkoutConfig.address_autocomplete.api_key;

    if (apiKey) {
        var autocomplete;

        function initAutocomplete() {
            var input = document.getElementById('street_1');
            autocomplete = new google.maps.places.Autocomplete(input, { types: ['geocode'] });
            autocomplete.addListener('place_changed', fillInAddress);
        }

        function fillInAddress() {
            var place = autocomplete.getPlace();
            // Handle filling in address fields
        }

        // Load Google Maps API
        var script = document.createElement('script');
        script.src = 'https://maps.googleapis.com/maps/api/js?key=' + apiKey + '&libraries=places&callback=initAutocomplete';
        
        // Error handling for script loading
        script.onerror = function () {
            console.error("`API .js Error`: `Failed to Load` - Trace Technical Error Code - Check Billing Status / Apply Verified Domain / API Key Usage - Check Quota - Rate Limits IP Firewall Block ://localhost:8080 / Rectify Parameters / Seek Community Help");
        };

        document.head.appendChild(script);
    }
});
10. Create `autocomplete.phtml`
File: app/code/Magento/AddressAutoComplete/templates/address/autocomplete.phtml
<div>
    <input type="text" id="autocomplete" placeholder="Enter your address" />
</div>

Google Places API - Maps Enterprise API - Address autocomplete functionality works as expected on the checkout page.

`Optional - Style Customization as per Maps Display - (Optional Places Markers and Style Embed) - Snazzy Maps API (Business - Single Site License) - by Adam Krogh

Type of API used - Google Maps JavaScript API (Add Custom Styles to Embedded Maps across CMS)
You can enable Paid API calls via Google Cloud Console Dashboard - Google Maps Platform API - Google Autocomplete (Third-party) (Address Verification) (Limit results to Country)

Google Maps JavaScript API is different from Place Autocomplete (Legacy) / Migrate to Place Autocomplete (New)
google API - URL - https://console.cloud.google.com/marketplace/product/google/maps-backend.googleapis.com?inv=1&invt=Ab0NGA
Google Enterprise API - URL (legacy support) - https://console.cloud.google.com/marketplace/product/google/places-backend.googleapis.com?inv=1&invt=Ab0NGA
Google Enterprise API - URL (migrate to latest) - https://console.cloud.google.com/marketplace/product/google/places.googleapis.com?inv=1&invt=Ab0NGA
Autocomplete - In the News - View Article James Harrison, Product Manager at Google published Feb 21, 2024
Next generation Autocomplete is now available in Preview
https://mapsplatform.google.com/resources/blog/next-generation-autocomplete-is-now-available-in-preview/

dravasp/addressautocomplete 适用场景与选型建议

dravasp/addressautocomplete 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 06 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「google」 「location api」 「Address Verification」 「magento commerce」 「atmanirbhar bharat india」 「uae uk usa india ksa」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 dravasp/addressautocomplete 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dravasp/addressautocomplete 我们能提供哪些服务?
定制开发 / 二次开发

基于 dravasp/addressautocomplete 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: OSL-3.0
  • 更新时间: 2025-06-16