/custom-currency

Custom Currency for Magento 2

Primary LanguagePHPMIT LicenseMIT

Custom Currency for Magento 2

  • No critical rewrites.
  • Add as many currencies as you want.
  • Preloaded with the general purpose currency Point and some of top cryptocurencies: Bitcoin (BTC), Ethereum (ETH), Bitcoin Cash (BCH), Ripple (XRP), Bitcoin Gold (BTG), DASH, Litecoin (LTC), IOTA, Ethereum Classic (ETC), Monero (XMR), Cardano (ADA), NEO, NEM, Stellar Lumen (XLM), Qtum (QTUM), Zcash (ZEC).

You can add your own currencies from the admin page or via dependency injection XML:

Admin Page

Visit Stores > Configuration > EcommPro > Custom Currency and add the currencies you want in the text box, separated by blank lines, with the format:

{code}
{singular}
{plural}

Example:

XMPL
Examplium
Exampliums

DOGE
DogeCoin
DogeCoins

Dependency Injection XML

If you want your currencies be available at install time, this should be the chosen method.

Add the currencies as the array of arrays argument of the EcommPro\CustomCurrency\Model\Config object constructor:

<type name="EcommPro\CustomCurrency\Model\Config">
    <arguments>
        <argument name="currencies" xsi:type="array">
            <item name="XMPL" xsi:type="array">
                <item name="code" xsi:type="string">XMPL</item>
                <item name="singular" xsi:type="string">Examplium</item>
                <item name="plural" xsi:type="string">Exampliums</item>
            </item>
            <item name="DOGE" xsi:type="array">
                <item name="code" xsi:type="string">DOGE</item>
                <item name="singular" xsi:type="string">DogeCoin</item>
                <item name="plural" xsi:type="string">DogeCoins</item>
            </item>
        </argument>
    </arguments>
</type>

Internals

This module is based in these observations:

  • Magento class loader doesn't load a class if it was previously loaded.
  • Magento doesn't check the type of PHP ResourceBundle returned by Magento\Framework\Locale\Bundle\CurrencyBundle and simply uses it as an array, as it implements array access and iteration.
  • In magento core files, Magento\Framework\Locale\Bundle\CurrencyBundle is always instantiated via constructor with new: (new CurrencyBundle())
grep -hr CurrencyBundle vendor/*
use Magento\Framework\Locale\Bundle\CurrencyBundle;
            $currencies = (new CurrencyBundle())->get($this->localeResolver->getLocale())['Currencies'];
use Magento\Framework\Locale\Bundle\CurrencyBundle as CurrencyBundle;
                        $allCurrencies = (new CurrencyBundle())->get(
class CurrencyBundle extends DataBundle
use Magento\Framework\Locale\Bundle\CurrencyBundle;
        $currencies = (new CurrencyBundle())->get($this->localeResolver->getLocale())['Currencies'] ?: [];
        $currencyBundle = new \Magento\Framework\Locale\Bundle\CurrencyBundle();
use Magento\Framework\Locale\Bundle\CurrencyBundle;
        $currencies = (new CurrencyBundle())->get(Resolver::DEFAULT_LOCALE)['Currencies'];

So, the key idea is to load our own version of Magento\Framework\Locale\Bundle\CurrencyBundle (a really simple class), override the get method and return a modified array imitating the original ResourceBundle.

User Guide

Description

EcommPro Custom Currency allows you to create and edit new currencies directly from the Magento administrator. Manage the options of these currencies such as the precision of decimals, the symbol as image, emoji or text, position, etc. Advisable for cryptocurrency.

Regarding currencies, Magento 2 has a lack.

The currency system is heavily dependent on the PHP intl ICU extension.

This is not bad. The intl extension is battle tested and powerful.

But it covers only official fiat currencies.

What about using custom currencies like points? Or what about the increasingly popular crypto currencies like Bitcoin (BTC), Tron (TRX), Ripple (XRP), among others?

At the moment there’s no a solution provided from the Magento 2 core.

We’ve created a module to manage custom currencies, preloaded with some useful data like points currency and some of the most popular cryptos.

We’ve decided to publish it in GitHub as open source.

https://github.com/ecommpro/custom-currency

We hope it is useful for you.

What can I do with EcommPro Custom Currency?

  • Create as many coins as you need.
  • Enables, disables or eliminates coins.
  • Give them a code, a name in the singular and in the plural.
  • Specifies the precision in decimals, ideal for cryptocurrency.
  • Specify the position (before or after the price).
  • Manage the symbol as text, emoji or image.

Installation

The .zip file contains two folders. Documentation where this user guide and Plugin is located. Copy the contents of the Plugin folder in the root of your Magento 2.

Execute the following commands

# (in case you have EcommPro_Core installed, run only php bin/magento module:enable EcommPro_CustomCurrency)
php bin/magento module:enable EcommPro_Core EcommPro_CustomCurrency

php bin/magento setup:upgrade

Access the Magento 2 administration panel and configure the module.

Module Guide

You can access the module options easily from the side menu, Stores > Currency > EcommPro Custom Currency.

You can list and manage new currencies easily from a Magento Grid.

Image 2

Customize your new coins with the variety of possible options such as number of decimals, position, name or symbol.

Image 3

From the Magento coin configuration section, select your new coins.

Image 4

Go to a listing or product listing and you will see your new currency in action.

Image 5

Help and information

If you need help or a specialized service you can receive support by writing an email from the following form: https://ecomm.pro/contact/