0

I want to use current currency symbol in custom module Js.

define([
'jquery',
'ko',
'uiComponent'
], function ($, ko, component) {
    'use strict';
    return component.extend({
        initialize: function () {
            this._super();
            this._render();
        },
        _render:function(){
            var self = this;
            console.log("need current currency symbol here.");
        },
        defaults: {
            template: 'Namespace_Modulename/product/view/price',
        },
    });
});

Any suggestion would be appreciated.

Thanks

  • I don't have time to write an answer sorry, but if you have access to the currency symbol you can pass it through to your JS file. Check this answer on how to do that – Ben Crook Nov 30 '17 at 13:20
  • 1
    Thanks for your answer @BenCrook I have passed currency symbol through Json and now I can access this in Js file. – Chander Shekhar Nov 30 '17 at 13:28
0

For this first you have to create a block file as below:

<?php
class Customclass{
    protected $_currency;
    public function __construct(        
        \Magento\Directory\Model\Currency $currency,
    ){       
        $this->_currency = $currency;
    }

    public function getStoreCurrencySymbol(){
        return $this->_currency->getCurrencySymbol();
    }
}

In it's template you need to call above function and pass the value to your js file as below:

<script>
    window.getCurrency = {
            "getCurrencySymbol":<?php /* @noEscape */ echo $block->getStoreCurrencySymbol(); ?>
        };
</script>
<div class="my-class">
    <div id="my-id" data-bind="scope: 'curency-symbol'">
        <!-- ko template: getTemplate() --><!-- /ko -->
        <script type="text/x-magento-init">
        {
            "#decoration-tier-price": {
                "Magento_Ui/js/core/app": <?php /* @escapeNotVerified */ echo $block->getJsLayout(); ?>
            }
        }
        </script>
    </div>
</div>

create your js file as below where you will get your current currency symbol.

define([
'jquery',
'ko',
'uiComponent'
], function ($, ko, component) {
  'use strict';

  return component.extend({
    initialize: function () {
      this._super();
      this._render();
    },
    _render: function () {
      var self = this;
      var getCurency = window.getCurrency;
      console.log(getCurency.getCurrencySymbol)
    },
    defaults: {
      template: 'namespace_modulename/template/path',
    },
  });
});
1

i told you one think you have get currency in store collection then store any varible then this variable you use in script tags same like this example:-var num = <?php echo $num ?>; i hope you are understand this way think and do this way :) and current store currency please you refrence this website http://blog.chapagain.com.np/magento-2-get-currency-code-currency-symbol-currency-rate/

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.