Magento Tutorials

How to get Currency data: Code, Rate, Symbol in Magento 2

Hello Magento Folks,

In this tutorial blog, I will help you to get the currency data like code, rate, and symbol in Magento 2. Also, check out my last published blog where I have illustrated How to Add Discount Component to Checkout Order Summary in Magento 2. Let’s get started with today’s article.

Basically when anyone utilizes the Magento 2 platform to develop an e-commerce/online store then there is a functionality where you can accept any currencies which depend on your customers and buyers. With the help of this illustration, I will assist you to fetch default, current currency code available currency code as well as allowed currency codes.

Steps to get Currency data: Code, Rate, Symbol in Magento 2:

Step 1: Declare in Vendor_Extension

You will use a block class of the module Vendor_Extension, then possibly inject the object of StoreManagerInterface & Currency class in the constructor of the module’s block class.

Path: app/code/Vendor/Extension/Block/Currency/Getdata.php

<?php

namespace Vendor\Extension\Block\Currency;

use Magento\Backend\Block\Template\Context;

use Magento\Store\Model\StoreManagerInterface;

use Magento\Directory\Model\Currency;

class Getdata extends \Magento\Framework\View\Element\Template

{

    protected $storeManager;

    protected $currency;

             public function __construct(

        Context $context,

        StoreManagerInterface $storeManager,

        Currency $currency,

        array $data = [])

    {

        $this->storeManager = $storeManager;

        $this->currency = $currency;

        parent::__construct($context, $data);

    }

    public function getCurrentStore()

    {

        return $this->storeManager->getStore();

    }

    public function getBaseCurrencyCode()

    {

        return  $this->getCurrentStore()->getBaseCurrencyCode();

    }

    public function getCurrentCurrencyCode()

    {

        return$this->getCurrentStore()->getCurrentCurrencyCode();

    }

    public function getDefaultCurrencyCode()

    {

        return $this->getCurrentStore()->getDefaultCurrencyCode();

    }

    public function getAvailableCurrencyCodes($BaseNotAllowed = false)

    {

        return $this->getCurrentStore()->getAvailableCurrencyCodes($BaseNotAllowed);

    }

    public function getAllowedCurrencies()

    {

        return $this->getCurrentStore()->getAllowedCurrencies();

    }

    public function getCurrentCurrencyRate()

    {

        return $this->getCurrentStore()->getCurrentCurrencyRate();

    }

    public function getCurrentCurrencySymbol()

    {

        return $this->currency->getCurrencySymbol();

    }

}

 

You can see more functions in vendor/magento/module-store/Model/Store.php and vendor/magento/module-directory/Model/Currency.php.

Step 2: Now, we will fetch the output in the currency data in the phtml file.

Run the below code for fetching and printing the currency symbol, currency code, and currency rate in the template phtml file. 

currencyData.phtml

 

<?php

echo $block->getCurrentCurrencySymbol() . '<br />';

echo $block->getCurrentCurrencyCode() . '<br />';

echo $block->getBaseCurrencyCode() . '<br />';

echo $block->getDefaultCurrencyCode() . '<br />';

echo $block->getCurrentCurrencyRate() . '<br />';

print_r($block->getAvailableCurrencyCodes()) . '<br />';

print_r($block->getAllowedCurrencies()) . '<br />';

 

That’s It

Wrap Up:

Hopefully, all are able to get Currency data: Code, Rate, Symbol in Magento 2 by following the above illustration. In case of any errors you face in the implementation of the above code then let me know in the comment section below. You can also utilize our Hire Magento Developer Service for implementing the above tutorial precisely in your Magento 2 store.

Share the article with your Magento Developer Friends

Happy Reading!

Click to rate this post!
[Total: 5 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

19 hours ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

3 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

3 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

4 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

6 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We have brought exciting news for Magento store owners. Hyvä Themes recently released 1.3.6 and…

6 days ago