I want to Fetch Current Country Code in Cart Page Section (js)
-
What you are pointing to is currency code, not country code. So are you looking for current currency selected by customer?– KhushbuJul 3, 2019 at 12:18
4 Answers
What you are pointing to is currency code, not country code.
You can find current currency code as below:
<?php
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.– KhushbuJul 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">
<body>
<referenceContainer name="before.body.end">
<block class="Magento\Framework\View\Element\Template" template="vendor_module::before.phtml" name="before_body_js"/>
</referenceContainer>
</body>
</page>
Craete before.phtml file
app/code/vendor/Module/view/frontend/templates/before.phtml
<?php
$_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); ?>
}
}
}
</script>
Create requirejs-config.js
app/code/Vendor/Module/view/frontend/requirejs-config.js
var config = {
map: {
'*': {
'Vendor_Module/js/view/authentication',
'myjscomponent': 'Vendor_Module/js/customjs'
}
}
};
Create custom.js file
app/code/Vendor/Module/view/frontend/web/js/customjs.js
define([], function(){
'use strict';
return function(config, element){
console.log(config);
}
});
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:
$this->_storeManager->getStore()->getCurrentCurrencyCode()
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);