5

I want to format my price in template, and I find some place told about Magento_Catalog/js/price-box, But I don't know how to use it, Anyone can help me show me how to do.

  • have you create custom theme in your magento? – Rakesh Jesadiya Dec 21 '16 at 8:48
  • No need new theme, I just customized a part of cart template. – MrTo-Kane Dec 21 '16 at 9:01
  • Magento_Catalog/js/price-box this is show js file – Rakesh Jesadiya Dec 21 '16 at 9:02
3

1) Magento_Catalog/js/price-box is used for formatting price amount.

  function formatPrice(amount, format, isShowSign) {
     ......
  }
  • amount: is price amount (is a number)

  • format is a Object, having some property: decimalSymbol, groupLength, groupSymbol, pattern, precision, ... One the checkout page, we can check window.checkoutConfig.priceFormat

enter image description here

  • isShowSign: is a Boolean value. Will show +/- (add/minus) sign to the price.

2) The good example for using the price box js is in One Page Checkout.

One the checkout page, the checkout config is stored in a global Js variable. We can check in Browser Console: window.checkoutConfig

enter image description here

vendor/magento/module-checkout/view/frontend/web/js/view/summary/abstract-total.js
vendor/magento/module-checkout/view/frontend/web/js/view/estimation.js
vendor/magento/module-checkout/view/frontend/web/js/view/cart/shipping-rates.js

       /**
         * Format shipping price.
         * @returns {String}
         */
        getFormattedPrice: function (price) {
            return priceUtils.formatPrice(price, quote.getPriceFormat());
        },

There is a note: quote.getPriceFormat() gets the value from window.checkoutConfig.priceFormat object.

[EDIT] we can use:

<script>
        window.orderPriceFormat = <?php /* @escapeNotVerified */ echo $this->helper('Magento\Tax\Helper\Data')->getPriceFormat($block->getStore()); ?>;
</script>

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.