0

When any guest user comes at cart page I want to change the country drop-down based on guest geoIp location.

Here I am adding the actual picture.

enter image description here

  • 1
    write more, if you tried something add that too. – Sohel Rana May 11 '19 at 5:18
  • @SohelRana I have updated my queston. – Manish Goswami May 11 '19 at 6:05
  • @ManishGoswami specify your requirement into detail, what you want to do here? – Dhiren Vasoya May 11 '19 at 6:09
  • @DhirenVasoya I have updated my answer . – Manish Goswami May 11 '19 at 6:16
  • Have you tried anything? Or just ask some question to get solution and code?? – HoangHieu May 11 '19 at 17:25
2

Try to using a plugin that overwrites country list with your specific country list. I have used hard-coded country here, you need to add your logic here.

app/code/SR/MagentoCommunity/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Block\Cart\LayoutProcessor">
        <plugin name="sr_country_list"
                type="SR\MagentoCommunity\Plugin\Checkout\Block\Cart\LayoutProcessor" sortOrder="1"/>
    </type>
</config>

app/code/SR/MagentoCommunity/Plugin/Checkout/Block/Cart/LayoutProcessor.php

<?php
namespace SR\MagentoCommunity\Plugin\Checkout\Block\Cart;

class LayoutProcessor
{
    public function afterProcess(
        \Magento\Checkout\Block\Cart\LayoutProcessor $subject,
        $jsLayout
    ) {
        $selectedCountry = 'GB';
        if (isset($jsLayout['components']['checkoutProvider']['dictionaries'])) {

            foreach ($jsLayout['components']['checkoutProvider']['dictionaries']['country_id'] as &$country) {
                if ($country['value'] == $selectedCountry) {
                    $country['is_default'] = 1;
                } else {
                    if (isset($country['is_default'])) {
                        unset($country['is_default']);
                    }
                }
            }
        }
        if (isset($jsLayout['components']['block-summary']['children']['block-shipping']['children']['address-fieldsets'])) {
            $jsLayout['components']['block-summary']['children']['block-shipping']['children']['address-fieldsets']['children']['country_id']['value'] = $selectedCountry;
        }

        return $jsLayout;
    }
}

app/code/SR/MagentoCommunity/view/frontend/requirejs-config.js

var config = {
    config: {
        mixins: {
            'Magento_Checkout/js/view/cart/shipping-estimation': {
                'SR_MagentoCommunity/js/mixin/shipping-estimation-mixin': true
            }
        }
    }
};

app/code/SR/MagentoCommunity/view/frontend/web/js/mixin/shipping-estimation-mixin.js

define([
    'jquery',
    'Magento_Ui/js/form/form',
    'Magento_Checkout/js/action/select-shipping-address',
    'Magento_Checkout/js/model/address-converter',
    'Magento_Checkout/js/model/cart/estimate-service',
    'Magento_Checkout/js/checkout-data',
    'Magento_Checkout/js/model/shipping-rates-validator',
    'uiRegistry',
    'Magento_Checkout/js/model/quote',
    'Magento_Checkout/js/model/checkout-data-resolver',
    'Magento_Checkout/js/model/shipping-service',
    'Magento_Customer/js/model/customer',
    'mage/validation'
], function (
    $,
    Component,
    selectShippingAddress,
    addressConverter,
    estimateService,
    checkoutData,
    shippingRatesValidator,
    registry,
    quote,
    checkoutDataResolver,
    shippingService,
    customer
) {
    'use strict';

    return function (target) {
        return target.extend({
            initialize: function () {
                this._super();
                if (customer.isLoggedIn()) {
                    return;
                }

                registry.async('checkoutProvider')(function (checkoutProvider) {
                    var address, estimatedAddress;

                    shippingService.isLoading(false);

                    checkoutDataResolver.resolveEstimationAddress();
                    address = quote.isVirtual() ? quote.billingAddress() : quote.shippingAddress();

                    if (!address && quote.isVirtual()) {
                        address = addressConverter.formAddressDataToQuoteAddress(
                            checkoutData.getSelectedBillingAddress()
                        );
                    }
                    var selectedCountry = '';
                    $.each(checkoutProvider.dictionaries.country_id, function(index, item) {
                        if (item.is_default != undefined) {
                            selectedCountry = item.value;
                        }
                    });
                    if (address) {
                        estimatedAddress = address.isEditable() ?
                            addressConverter.quoteAddressToFormAddressData(address) :
                            {
                                // only the following fields must be used by estimation form data provider
                                'country_id': address.countryId,
                                region: address.region,
                                'region_id': address.regionId,
                                postcode: address.postcode
                            };

                        if (estimatedAddress.firstname == undefined && selectedCountry) {
                            estimatedAddress.country_id = selectedCountry;
                        }

                        checkoutProvider.set(
                            'shippingAddress',
                            $.extend({}, checkoutProvider.get('shippingAddress'), estimatedAddress)
                        );
                    }

                    if (!quote.isVirtual()) {
                        checkoutProvider.on('shippingAddress', function (shippingAddressData) {
                            checkoutData.setShippingAddressFromData(shippingAddressData);
                        });
                    } else {
                        checkoutProvider.on('shippingAddress', function (shippingAddressData) {
                            checkoutData.setBillingAddressFromData(shippingAddressData);
                        });
                    }
                });
            }
        });
    }
});
|improve this answer|||||
  • Check updated answer – Sohel Rana May 11 '19 at 7:20
  • Thanks @Sohel Rana Its working !!! – Manish Goswami May 11 '19 at 9:29
  • If customer is logged in then it over write customer country.I check is Customers logged in condition but getting js error. – Manish Goswami May 11 '19 at 13:58
  • How can I disable this functionality if customer is logged in. – Manish Goswami May 11 '19 at 14:01
  • I have updated js file. check now – Sohel Rana May 11 '19 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.