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 JesadiyaDec 21, 2016 at 8:48
-
No need new theme, I just customized a part of cart template.– MrTo-KaneDec 21, 2016 at 9:01
-
Magento_Catalog/js/price-box this is show js file– Rakesh JesadiyaDec 21, 2016 at 9:02
1 Answer
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 checkwindow.checkoutConfig.priceFormat
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
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>