How to Add Custom Currency to Magento 2?

How to Add Custom Currency to Magento 2

Hello Magento Friends,

In this blog, we will learn about How to Add Custom Currency to Magento 2.

Magento 2 is a powerful e-commerce platform that provides a plethora of features for managing online stores. One such feature is the ability to handle multiple currencies, which is crucial for businesses operating in international markets. Merchants can also add a currency switcher in store frontend to improve user’s shopping experience.

While Magento 2 supports a variety of currencies out of the box, there may be instances where you need to add a custom currency that isn’t natively supported. This could be for a loyalty program, a custom reward system, or even a unique virtual currency. 

This guide will walk you through the steps to add a custom currency to your Magento 2 store.

Steps to Add Custom Currency to Magento 2:

Step 1: Create a registration.php file at the following path.

/app/code/Vendor/Extension/registration.php

Now, add the below given code

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Extension',
    __DIR__
);
require_once(__DIR__ . '/Override/Currencybundle.php');

Step 2: Create Currencybundle.php file at the below path.

/app/code/Vendor/Extension/Override/Currencybundle.php

Then add the code as follows

<?php
namespace Magento\Framework\Locale\Bundle;
class Currencybundle extends DataBundle
{
    /**
     * @var string
     */
    protected $path = 'ICUDATA-curr';

    /**
     * @var string
     */
    public function toArray($bundle)
    {
        $aux = [];
        foreach ($bundle as $k => $v) {
            $aux[$k] = is_object($v) ? $this->toArray($v) : $v;
        }
        return $aux;
    }

    /**
     * Method to get currency bundle
     *
     * @param string $locale
     * @return array
     */
    public function get($locale)
    {
        $bundle = parent::get($locale);
        $bundleAsArray = $this->toArray($bundle);
        $bundleAsArray['Currencies']['BTC'] = [
            'BTC',
            'Bitcoin',
        ];
        $bundleAsArray['CurrencyPlurals']['BTC'] = [
            'one' => 'Bitcoin',
            'other' => 'Bitcoin',
        ];
        return $bundleAsArray;
    }
}

Step 3: Create di.xml file at the below-mentioned file path.

/app/code/Vendor/Extension/etc/di.xml

After that add the following piece of code

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Framework\Locale\Config">
        <plugin name="crypto_locale" type="Vendor\Extension\Plugin\Localeconfig" sortOrder="1" />
    </type>

    <type name="Magento\Framework\App\Config">
        <plugin name="crypto_config" type="Vendor\Extension\Plugin\Configplugin" sortOrder="1" />
    </type>

    <type name="Magento\Framework\Pricing\Render\Amount">
        <plugin name="crypto_framework_pricing_render_amount" type="Vendor\Extension\Plugin\Framework\Pricing\Render\Amount" />
    </type>

    <type name="Magento\Directory\Model\Pricecurrency">
        <plugin name="crypto_directory_model_pricecurrency" type="Vendor\Extension\Plugin\Directory\Model\Pricecurrency" />
    </type>
    
    <type name="Magento\Framework\Locale\Format">
        <plugin name="crypto_framework_locale_format" type="Vendor\Extension\Plugin\Framework\Locale\Format" />
    </type>
</config>

Step 4: Create Localeconfig.php file at the below-given file path.

/app/code/Vendor/Extension/Plugin/Localeconfig.php

Now, include the following code snippet

<?php
namespace Vendor\Extension\Plugin;
class Localeconfig
{
    /**
     * After get allowed currencies
     *
     * @param \Magento\Framework\Locale\Config $config
     * @param array $currencies
     * @return array
     */
    public function afterGetAllowedCurrencies(\Magento\Framework\Locale\Config $config, $currencies)
    {
        return array_merge($currencies, ['BTC']);
    }
}

Step 5: Create Configplugin.php file at the path given below.

/app/code/Vendor/Extension/Plugin/Configplugin.php

Then after, add the below code

<?php
namespace Vendor\Extension\Plugin;

class Configplugin
{
    /**
     * After get currency config value
     *
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $subject
     * @param string $result
     * @param string $path
     * @param string $scope
     * @param string $scopeCode
     * @return string
     */
    public function afterGetValue(\Magento\Framework\App\Config\ScopeConfigInterface $subject,
        $result,
        $path = null,
        $scope = \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
        $scopeCode = null
    )
    {
        if ($path === 'system/currency/installed') {
            return $result . ',' . 'BTC';
        }
        return $result;
    }
}

Step 6: Create Amount.php file at the following path.

/app/code/Vendor/Extension/Plugin/Framework/Pricing/Render/Amount.php

Now, include the following code

<?php
namespace Vendor\Extension\Plugin\Framework\Pricing\Render;

use Magento\Framework\Pricing\PriceCurrencyInterface;

class Amount
{
  public function beforeFormatCurrency(
    $subject,
    $amount,
    $includeContainer = true,
    $precision = PriceCurrencyInterface::DEFAULT_PRECISION
  ) {
    return [$amount, $includeContainer, 4];
  }
}

Step 7: Create Pricecurrency.php file at the below-mentioned path.

/app/code/Vendor/Extension/Plugin/Directory/Model/Pricecurrency.php

And then add the code as given below

<?php
namespace Vendor\Extension\Plugin\Directory\Model;

class Pricecurrency
{
  public function beforeConvertAndRound(
    $subject,
    $amount,
    $scope = null,
    $currency = null,
    $precision = \Magento\Directory\Model\PriceCurrency::DEFAULT_PRECISION
  ) {
    return [$amount, $scope, $currency, 4];
  }

  public function beforeFormat(
    $subject,
    $amount,
    $includeContainer = true,
    $precision = \Magento\Directory\Model\PriceCurrency::DEFAULT_PRECISION,
    $scope = null,
    $currency = null
  ) {
    return [$amount, $includeContainer, 4, $scope, $currency];
  }
}

Step 8: Create Format.php file at the path given below.

/app/code/Vendor/Extension/Plugin/Framework/Locale/Format.php

And add the code as follows

<?php
namespace Vendor\Extension\Plugin\Framework\Locale;

class Format
{
  public function afterGetPriceFormat($subject, $result) {
    $result['precision'] = 4;
    $result['requiredPrecision'] = 4;

    return $result;
  }
}

Output:

You can see that your custom currency has been added to the backend currency setup configuration.

backend custom currency

The custom currency will be displayed on the frontend product page.

product page custom currency

The custom currency will also be displayed on the frontend category page.

category page custom currency

Conclusion:

By following this guide, you can extend the capabilities of your Magento 2 store to support any custom currency, enhancing your store’s flexibility and catering to a broader audience.

Share the tutorial with your friends and stay in touch with us for more such Magento 2 custom solutions.

Happy Coding!

Previous Article

How to Install Hyvä Themes in Magento 2?

Next Article

What is Hyvä Themes for Magento?

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨