0

How to get Currency Code & Currency Rate inside controller? So that I can use it in my Ajax response and show it on the frontend.

Update: Now I am getting CurrencySymbol(the symbol is not changing for each store view ) also CurrencyRate is empty by using the below code

<?php
namespace Ishaq\CheckoutTotal\Controller\Index;

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Action;
 
class Index extends Action
{
    protected $resultPageFactory;
    protected $_storeManager;
    protected $_currency;
 
    public function __construct(
                                Context $context, 
                                PageFactory $resultPageFactory,
                                \Magento\Store\Model\StoreManagerInterface $storeManager,
                                \Magento\Directory\Model\Currency $currency
                                
    )
    {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
        $this->_storeManager = $storeManager;
        $this->_currency = $currency;
        
    }
 
    public function execute()
    {
        $post = $this->getRequest()->getPost();
        $data['getBaseCurrencyCode'] = $this->getBaseCurrencyCode();
        $data['getCurrentCurrencyCode'] = $this->getCurrentCurrencyCode();
        $data['getDefaultCurrencyCode'] = $this->getDefaultCurrencyCode();
        $data['getCurrentCurrencySymbol'] = $this->getCurrentCurrencySymbol();
        $data['getCurrentCurrencyRate'] = $this->getCurrentCurrencyRate();
        $data['store_view'] = $this->getStore()->getCurrentCurrency()->getCode();
        
        
        var_dump($data); exit;
        
        $response = $this->resultFactory
            ->create(\Magento\Framework\Controller\ResultFactory::TYPE_JSON)
            ->setData($post);
        return $response;
    }

    /**
     * Get store base currency code
     *
     * @return string
     */
    public function getBaseCurrencyCode()
    {
        return $this->_storeManager->getStore()->getBaseCurrencyCode();
    }
    
     /**
     * Get current store currency code
     *
     * @return string
     */
    public function getCurrentCurrencyCode()
    {
        return $this->_storeManager->getStore()->getCurrentCurrencyCode();
    }    
    
    /**
     * Get default store currency code
     *
     * @return string
     */
    public function getDefaultCurrencyCode()
    {
        return $this->_storeManager->getStore()->getDefaultCurrencyCode();
    }


    /**
     * Get current currency rate
     *
     * @return float
     */
    public function getCurrentCurrencyRate()
    {
        return $this->_storeManager->getStore()->getCurrentCurrencyRate();
    }
    
    /**
     * Get currency symbol for current locale and currency code
     *
     * @return string
     */    
    public function getCurrentCurrencySymbol()
    {
        return $this->_currency->getCurrencySymbol();
    }
    
    /* Get Store Name
       *
        * @return \Magento\Sales\Model\StoreManagerInterface
        */
       public function getStore() {
         $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
           $storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
           return $storeManager->getStore();
       }
       
       
}
-1

You can add \Magento\Store\Model\StoreManagerInterface and \Magento\Directory\Model\Currency in your controller and get currency code and rate this way:

<?php
namespace Ishaq\Module\Controller;

class YourController
{
    protected $_storeManager;
    protected $_currency;
    
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Directory\Model\Currency $currency
    )
    {        
        $this->_storeManager = $storeManager;
        $this->_currency = $currency;
    }
    
        
    /**
     * Get store base currency code
     *
     * @return string
     */
    public function getBaseCurrencyCode()
    {
        return $this->_storeManager->getStore()->getBaseCurrencyCode();
    }
    
     /**
     * Get current store currency code
     *
     * @return string
     */
    public function getCurrentCurrencyCode()
    {
        return $this->_storeManager->getStore()->getCurrentCurrencyCode();
    }    
    
    /**
     * Get default store currency code
     *
     * @return string
     */
    public function getDefaultCurrencyCode()
    {
        return $this->_storeManager->getStore()->getDefaultCurrencyCode();
    }


    /**
     * Get current currency rate
     *
     * @return float
     */
    public function getCurrentCurrencyRate()
    {
        return $this->_storeManager->getStore()->getCurrentCurrencyRate();
    }
    
    /**
     * Get currency symbol for current locale and currency code
     *
     * @return string
     */    
    public function getCurrentCurrencySymbol()
    {
        return $this->_currency->getCurrencySymbol();
    }
}
    

Reference: https://www.mageplaza.com/devdocs/how-get-currency-data-code-rate-symbol-magento-2.html

1
  • I am able to get data but not as expected, I am changing store but getCurrentCurrencySymbol still remains the same.
    – ishaq
    Feb 4 at 5:11

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.