0

I have multiple stores on my website. I need to get the store currency code by store Id. How can I get it?

I was trying by below code but it's always returning default store currency code:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');       
$storeCurrencyCode = $storeManager->getStore()->getCurrentCurrencyCode();

Thanks for your cooperation.

[Update with Solution]

I have got the solution after reviewing Magento core code. My solution code below:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();       
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');

// Return Default Store Currency Code
$defaultCurrencyCode = $storeManager->getStore()->getCurrentCurrencyCode();

// Return Specific Store Currency Code
$storeId = 1; // it can be any store id     
$storeCurrencyCode = $storeManager->getStore($storeId)->getCurrentCurrencyCode();

Thanks for your time.

0

Best way to create a block to get currency code and pass to template. Please do not directly use the object manager in code.

Default Store Currency data: Code, Rate, Symbol.

<?php
namespace Vendor\Module\Block;
use Magento\Framework\View\Element\Template;

class DefaultStoreCurrencyBlock extends Template
{
    protected $_storeManager;
    protected $_currency;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Directory\Model\Currency $currency,        
        array $data = []
    )
    {        
        $this->_storeManager = $storeManager;
        $this->_currency = $currency;        
        parent::__construct($context, $data);
    }

    public function getBaseCurrencyCode()
    {
        return $this->_storeManager->getStore()->getBaseCurrencyCode();
    }

    public function getCurrentCurrencyCode()
    {
        return $this->_storeManager->getStore()->getCurrentCurrencyCode();
    }    

    public function getDefaultCurrencyCode()
    {
        return $this->_storeManager->getStore()->getDefaultCurrencyCode();
    }

    public function getAllowedCurrencies()
    {
        return $this->_storeManager->getStore()->getAllowedCurrencies();
    }

    public function getCurrentCurrencyRate()
    {
        return $this->_storeManager->getStore()->getCurrentCurrencyRate();
    }

    public function getCurrentCurrencySymbol()
    {
        return $this->_currency->getCurrencySymbol();
    }    
}
?>

Specific Store Currency Code: Code, Rate, Symbol.

<?php
namespace Vendor\Module\Block;
use Magento\Framework\View\Element\Template;

class SpecificStoreCurrencyBlock extends Template
{
    protected $_storeManager;
    protected $_currency;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Directory\Model\Currency $currency,        
        array $data = []
    )
    {        
        $this->_storeManager = $storeManager;
        $this->_currency = $currency;        
        parent::__construct($context, $data);
    }

    public function CurrentStoreID()
    {
        return $this->_storeManager->getStore()->getStoreId();
    }

    public function getBaseCurrencyCode()
    {  
        $storeId = $this->CurrentStoreID();
        return $this->_storeManager->getStore($storeId)->getBaseCurrencyCode();
    }

    public function getCurrentCurrencyCode()
    {
        $storeId = $this->CurrentStoreID();
        return $this->_storeManager->getStore($storeId)->getCurrentCurrencyCode();
    }    

    public function getDefaultCurrencyCode()
    {
        $storeId = $this->CurrentStoreID();
        return $this->_storeManager->getStore($storeId)->getDefaultCurrencyCode();
    }

    public function getAllowedCurrencies()
    {
        $storeId = $this->CurrentStoreID();
        return $this->_storeManager->getStore($storeId)->getAllowedCurrencies();
    }

    public function getCurrentCurrencyRate()
    {
        $storeId = $this->CurrentStoreID();
        return $this->_storeManager->getStore($storeId)->getCurrentCurrencyRate();
    }

}
?>

Get the output of the currency data in phtml file

echo $block->getCurrentCurrencySymbol() . '<br />';
echo $block->getCurrentCurrencyCode() . '<br />';
echo $block->getBaseCurrencyCode() . '<br />';
echo $block->getDefaultCurrencyCode() . '<br />';
echo $block->getCurrentCurrencyRate() . '<br />';
| improve this answer | |
  • Thanks for your answer, I don't need it in the template. I needed it in the Cron file where I was importing products programmatically from third-party API. – Benzir Hasan May 10 at 6:32
  • you can also use above code in cron file – Devidas May 10 at 6:50
  • How to pass store id dynamically in corn file using your solution? where this function using to get store id: public function CurrentStoreID() { return $this->_storeManager->getStore()->getStoreId(); } – Benzir Hasan May 10 at 19:39

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.