5

I am working on Magento 2 Reward Points section and want to show price in points on storefront.

I have product price $25 on storefront And I have set Reward Exchange Rates

$1 = 10 points

10 points = 1$

Now the product price should be ( 250 points )

also currency symbols should show points instead of $

I am trying to achieve this from following method but stucks

class Magento\Directory\Model\PriceCurrency

/**
 * {@inheritdoc}
 */
public function convert($amount, $scope = null, $currency = null)
{
    $currentCurrency = $this->getCurrency($scope, $currency);

    return $this->getStore($scope)
        ->getBaseCurrency()
        ->convert($amount, $currentCurrency);
}

Note: Price should be converted in points everywhere on store front.

If you guys have any Idea please share

Any help will be appreciated

  • Put some efforts at Least provide more description to question so that someone ca understand itt. – Vivek Nov 22 '18 at 12:35
  • @Vivek I have update my question. – Chander Shekhar Nov 22 '18 at 13:05
  • Hiya! Not a complete solution but might an idea. 1. Make a custom module 2. Create needed functions that converts prices into points 3. Create an override for templates, use your points conversion method – Andreas Therkildsen Nov 24 '18 at 21:38
  • which issue you are facing? – Dhiren Vasoya Nov 26 '18 at 13:57
  • @DhirenVasoya I want to convert price into reward points on store front if reward module is enabled. – Chander Shekhar Nov 26 '18 at 14:31
3
+25

To show price as points , You can implement following logic

  • create a custom currency "Reward Points"

  • Allow "Reward Points" custom currency from admin panel currency
    settings

  • Set currency set for "Reward Points" 10 = 1 USD

  • set Default Display Currency to "Reward Points"

We accused Custom Currency code for Reward Points is "PTS" (as "INR" for Indian Rupee)

You can follow below steps to implement above logic.

step 1) open YOUR-MAGENTO-ROOT-DIRECTORY/vendor/magento/zendframework1/library/Zend/Locale/Data/en.xml

Under currencies xml entity , enter your custom currency entiry (i.e "Reward Points") as follows and save the xml file.

<currencies>
----------------
<currency type="PTS">
   <displayName>Reward Points</displayName>
   <displayName count="one">Reward Points</displayName>
   <displayName count="other">Reward Points</displayName>
   <symbol>points</symbol> 
</currency>
----------------
</currencies>

step 2: open YOUR-MAGENTO-ROOT-DIRECTORY/vendor/magento/zendframework1/library/Zend/Locale/Data/en.xml

under installed XML entity enter your custom currency code as below

<system>
<currency>
<installed>PTS,AZN,AZM,AFN,ALL,DZD,AOA,ARS,AMD,AWG,AUD,BSD,BHD,BDT,BBD,BYR,BZD,BMD,BTN,BOB,BAM,BWP,BRL,GBP,BND,BGN,BUK,BIF,KHR,CAD,CVE,CZK,KYD,CLP,CNY,COP,KMF,CDF,CRC,HRK,CUP,DKK,DJF,DOP,XCD,EGP,SVC,GQE,ERN,EEK,ETB,EUR,FKP,FJD,GMD,GEK,GEL,GHS,GIP,GTQ,GNF,GYD,HTG,HNL,HKD,HUF,ISK,INR,IDR,IRR,IQD,ILS,JMD,JPY,JOD,KZT,KES,KWD,KGS,LAK,LVL,LBP,LSL,LRD,LYD,LTL,MOP,MKD,MGA,MWK,MYR,MVR,LSM,MRO,MUR,MXN,MDL,MNT,MAD,MZN,MMK,NAD,NPR,ANG,TRL,TRY,NZD,NIC,NGN,KPW,NOK,OMR,PKR,PAB,PGK,PYG,PEN,PHP,PLN,QAR,RHD,RON,ROL,RUB,RWF,SHP,STD,SAR,RSD,SCR,SLL,SGD,SKK,SBD,SOS,ZAR,KRW,LKR,SDG,SRD,SZL,SEK,CHF,SYP,TWD,TJS,TZS,THB,TOP,TTD,TND,TMM,USD,UGX,UAH,AED,UYU,UZS,VUV,VEB,VEF,VND,CHE,CHW,XOF,XPF,WST,YER,ZMK,ZWD</installed>
</currency>
</system>

step 3: Create a custom extension or use any exiting custom extension

step 4: we assumed , we are using custom extension Company_MyModule

step 5: craete file AddCurrencies.php under app/code/Company/MyModule/Model/Config

File AddCurrencies.php

<?php
namespace Company\MyModule\Model\Config;

use Magento\Framework\Locale\Bundle\CurrencyBundle;

class AddCurrencies extends \Magento\Framework\Locale\TranslatedLists
{

    public function getNewCurrencies() 
    {
        /* 
           This is your function that returns an array with new
           currencies. For example: 
         */
        return [
            ['value' => 'PTS', 'label' => 'Reward Points'],            
        ];
    }

    public function getOptionAllCurrencies()
    {
        $currencyBundle = new \Magento\Framework\Locale\Bundle\CurrencyBundle();
        $locale = $this->localeResolver->getLocale();
        $currencies = $currencyBundle->get($locale)['Currencies'] ?: [];

        $options = [];
        foreach ($currencies as $code => $data) {
            $options[] = ['label' => $data[1], 'value' => $code];
        }
        $options = array_merge($options, $this->getNewCurrencies());

        return $this->_sortOptionArray($options);
    }

    public function getOptionCurrencies()
    {
        $currencies = (new CurrencyBundle())->get($this->localeResolver->getLocale())['Currencies'] ?: [];
        $options = [];
        $allowed = $this->_config->getAllowedCurrencies();
        foreach ($currencies as $code => $data) {
            if (!in_array($code, $allowed)) {
                continue;
            }
            $options[] = ['label' => $data[1], 'value' => $code];
        }
        $options = array_merge($options, $this->getNewCurrencies());

        return $this->_sortOptionArray($options);
    }


}

step 6: Run following CLI command from your Magento 2 root directory to clear static contend and generated files

sudo rm -rf var/view_preprocessed/*
sudo rm -rf var/pub/static/*
sudo rm -rf var/cache/*
sudo rm -rf var/generated

step 7: Again run following CLI command from your Magento 2 root directory

sudo php bin/magento setup:upgrade
sudo php bin/magento setup:di:compile
sudo chmod -Rv 777 pub var generated

step 8: Please go to admin panel and navigate Stores->Configuration-> Currency Setup (under general tab) Select "Reward Points" and "US Dollar" as Allowed Currencies. now click "Save Config" button.

Step 9: Please go to Admin panel Stores-> Currency Rates Now enter Currency rate for "PTS" (i.e Reward Points) as 10.00 = 1 USD click "Save Currency Rates"

Step 10: Refresh Cache from System-> Cache Management

step 11: To display Price in Points, Please go to admin panel and navigate Stores->Configuration-> Currency Setup (under general tab) set Default Display Currency = Reward Points click "Save Config"

step 12: Please refresh cache again to show price in Points

Allowed Currency setting

Price With USD

enter image description here

0

@Pritam Info 24

I'm trying to Implement this but the Currency Reward Points doesn't show up in list of currencies..

This node was not present in my YOUR-MAGENTO-ROOT-DIRECTORY/vendor/magento/zendframework1/library/Zend/Locale/Data/en.xml

 <system>
    <currency>
    <installed>PTS,AZN,AZM,AFN,ALL,DZD,AOA,ARS,AMD,AWG,AUD,BSD,BHD,BDT,BBD,BYR,BZD,BMD,BTN,BOB,BAM,BWP,BRL,GBP,BND,BGN,BUK,BIF,KHR,CAD,CVE,CZK,KYD,CLP,CNY,COP,KMF,CDF,CRC,HRK,CUP,DKK,DJF,DOP,XCD,EGP,SVC,GQE,ERN,EEK,ETB,EUR,FKP,FJD,GMD,GEK,GEL,GHS,GIP,GTQ,GNF,GYD,HTG,HNL,HKD,HUF,ISK,INR,IDR,IRR,IQD,ILS,JMD,JPY,JOD,KZT,KES,KWD,KGS,LAK,LVL,LBP,LSL,LRD,LYD,LTL,MOP,MKD,MGA,MWK,MYR,MVR,LSM,MRO,MUR,MXN,MDL,MNT,MAD,MZN,MMK,NAD,NPR,ANG,TRL,TRY,NZD,NIC,NGN,KPW,NOK,OMR,PKR,PAB,PGK,PYG,PEN,PHP,PLN,QAR,RHD,RON,ROL,RUB,RWF,SHP,STD,SAR,RSD,SCR,SLL,SGD,SKK,SBD,SOS,ZAR,KRW,LKR,SDG,SRD,SZL,SEK,CHF,SYP,TWD,TJS,TZS,THB,TOP,TTD,TND,TMM,USD,UGX,UAH,AED,UYU,UZS,VUV,VEB,VEF,VND,CHE,CHW,XOF,XPF,WST,YER,ZMK,ZWD</installed>
    </currency>
    </system>

Please help.

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.