Get Default Country Value from the Current Website Using Programmatic way in Magento 2.
You can get the value of default country from the admin panel,
1. Stores -> Configuration -> General -> General
2. Country Options Tab -> Default Country.
Check the Selected Country value for the current website by set Store view as website scope.
You can get the country code for the current website by programmatically using the code snippet.
<?php namespace Jesadiya\Country\Model; use Magento\Store\Model\ScopeInterface; use Magento\Framework\App\Config\ScopeConfigInterface; class CountryByWebsite { /** * Get country path */ const COUNTRY_CODE_PATH = 'general/country/default'; /** * @var ScopeConfigInterface */ private $scopeConfig; public function __construct( ScopeConfigInterface $scopeConfig, ) { $this->scopeConfig = $scopeConfig; } /** * Get Country code by website scope * * @return string */ public function getCountryByWebsite(): string { return $this->scopeConfig->getValue( self::COUNTRY_CODE_PATH, ScopeInterface::SCOPE_WEBSITES ); } }
Call from the template or any PHP class,
general/country/default is the config path to retrieve country code.
echo $countryName = $this->getCountryByWebsite();
Output: (Two Digit Country code)
US (United States is selected)