How To

How to Set Price Format using JavaScript in Magento 2

Hello Magento Friends,

Today I am going to explain How to Set Price Format using JavaScript in Magento 2.

At the time of Magento 2 development, you may need to pass the price amount to js and display the currency from the js. At that time you can change the price format using JavaScript in Magento 2. You can convert plain number into price format with currency symbol using JavaScript in Magento 2.

Steps to Set Price Format using JavaScript in Magento 2:

Step 1: You need to use the below Magento file

Magento_Catalog\js\price-utils

If you have already created a javascript file then just add the getFormattedPrice() function and define the Magento_Catalog\js\price-utils file into js.

File Path – app\code\Vendor\Extension\view\frontend\web\js\checkpriceformate.js

define([
    'ko',
    'Magento_Catalog/js/price-utils'
], function (
    ko,
    priceUtils
) {
    'use strict';

    return {

        getFormattedPrice: function (price)
        {
            var priceFormat = {
                decimalSymbol: '.',
                groupLength: 3,
                groupSymbol: ",",
                integerRequired: false,
                pattern: "$%s",
                precision: 2,
                requiredPrecision: 2
            };

            return priceUtils.formatPrice(price, priceFormat);
        }
    }
});

Step 2: After that call getFormattedPrice() function where you want and it returns the price as per the below format.

Price Formate : $12.00

Step 3: After adding this code run the below commands

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush

Conclusion:

Accordingly, you can set the price format with the help of JavaScript in Magento 2. You may also like to read – Get Formatted Price with Currency using Block File in Magento 2

In case of any issues, you can reach out to me via the comment box. Share the article on your social media platforms and stay updated with us!

Happy Coding!

Click to rate this post!
[Total: 2 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

2 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

4 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

4 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

5 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

5 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago