I want to Fetch Current Country Code in Cart Page Section (js)

enter image description here

  • What you are pointing to is currency code, not country code. So are you looking for current currency selected by customer?
    – Khushbu
    Jul 3, 2019 at 12:18

4 Answers 4


What you are pointing to is currency code, not country code.

You can find current currency code as below:

use Magento\Store\Model\StoreManagerInterface;
class Custom
     private $storeConfig;

     public function __construct(
        StoreManagerInterface $storeConfig

    ) {
        $this->storeConfig = $storeConfig;


    public function getCurrentCurrency()
        /*** this is how you can get current currency code ***/
        $currentCurrency = $this->storeConfig->getStore()->getCurrentCurrencyCode();
        return currentCurrency;
  • @khushbu- Actually i need countrycode of the current currency code. Jul 3, 2019 at 12:36
  • For that you will need to create custom currency-country mapping table and develop custom module which can find out country for selected currency from that table. because magento does not provide such built in mapping. User can select Euro currency and enter India's shipping address!! there is no such restriction.
    – Khushbu
    Jul 3, 2019 at 12:59

Create default.xml file and write below code.

 <?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceContainer name="before.body.end">
            <block class="Magento\Framework\View\Element\Template" template="vendor_module::before.phtml" name="before_body_js"/>

Craete before.phtml file


$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $currencysymbol = $_objectManager->get('Magento\Store\Model\StoreManagerInterface');
    $currencyCode = $currencysymbol->getStore()->getCurrentCurrencyCode();
    $currency = $_objectManager->create('Magento\Directory\Model\CurrencyFactory')->create()->load($currencyCode); 
    $currencySymbol = $currency->getCurrencySymbol();

<script type="text/x-magento-init">
"*": {
    "myjscomponent": {
        "myjscomponent": <?php echo json_encode($currencyCode); ?>

Create requirejs-config.js


var config = {
map: {
    '*': {
        'myjscomponent': 'Vendor_Module/js/customjs'

Create custom.js file


define([], function(){
'use strict';

return function(config, element){


Similar question, https://stackoverflow.com/questions/22064773/is-there-any-possibilities-of-getting-country-name-using-currency-code-in-ma

But seems there is not any direct way to get the country code from currency.

You can achieve this by adding predefined currency - country code in the database table or achieve by preparing array and get country code by current currency.


Its a little unclear if you are after the country code as you suggested or the currency code as the example you have highlighted uses.

To get the current store currency code you can use \Magento\Store\Model\StoreManagerInterface. E.g in the scope of a block:


To get the current store country code you can use \Magento\Framework\App\Config\ScopeConfigInterface, in the scope of a block that would be

$this->_scopeConfig->getValue('general/country/default', ScopeInterface::SCOPE_STORE);

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.