Question

I am trying to add a googlemaps path to my requirejs-config file, but I need to put the locale on the end of the string. Here is what my requirejs-config file looks like. I need a way to get the local in javascript to put on the end of this url.

var config = {
    'map': {
        "*": {
            'googlemaps': '//maps.googleapis.com/maps/api/js?key=mykey&libraries=places&language=' + NEED-LOCAL-HERE
        }
    }
};

I would like to do this inside of //Magento_Checkout/web/template/shipping-address/form.html. This is what I have in that file right now.

<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<form class="form form-shipping-address" id="co-shipping-form" data-bind="attr: {'data-hasrequired': $t('* Required Fields')}">
    <!-- ko foreach: getRegion('before-fields') -->
    <!-- ko template: getTemplate() --><!-- /ko -->
    <!--/ko-->
    <div id="shipping-new-address-form" class="fieldset address">
        <!-- ko foreach: getRegion('additional-fieldsets') -->
        <!-- ko template: getTemplate() --><!-- /ko -->
        <!--/ko-->
        <!-- ko if: (isCustomerLoggedIn) -->
        <div class="field choice" data-bind="visible: !isFormInline">
            <input type="checkbox" class="checkbox" id="shipping-save-in-address-book" data-bind="checked: saveInAddressBook" />
            <label class="label" for="shipping-save-in-address-book">
                <span data-bind="i18n: 'Save in address book'"></span>
            </label>
        </div>
        <!-- /ko -->
    </div>
</form>

<script type="text/x-magento-init">
    {
        "*": {
            "shippingAddress": {
                "googlemapskey": "https//maps.googleapis.com/maps/api/js?key=mykey&libraries=places&language=PUT_LOCALE_HERE"
            }
        }
    }
</script>
Was it helpful?

Solution

You should pass the value in the template

In your .phtml template

require([
            "https//maps.googleapis.com/maps/api/js?key=mykey&libraries=places&language=<?php echo $block->getLanguageCode() ?>"
        ], function () {
            // Your code here
        });

You need to build the method getLanguageCode() in your block. You can pass your api key in template: key=mykey.

There are some extensions about google map. You can download and see the source code

https://github.com/mageplaza/magento-2-google-maps

https://amasty.com/google-map-for-magento-2.html

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top