Magento Tip : How to get the store price format by javascript ?

Some days before, I was working on a online ecommerce store. Fortunately it was a magento based ecommerce project. I was creating some new feature for the online store. I came across one minor problem. So the problem was if someone is changing the quantity amount, the product subtotal and total price should also be updated. Now it is very easy if we know the product unit price, we can multiply the quantity amount and hurry we got the subtotal price and accordingly we can calculate the total price also. But how to format the price, as this is a javascript code calculation. Ok here we go.

In magento we have a file named as js.js present inside “js/varien/” directory. In this file we have a method “formatCurrency”, which is meant for formating the price in magento store by javascript. It takes 3 parameters.

  • price
  • format[JSON (pattern, decimal, decimalsDelimeter, groupsDelimeter)]
  • showPlus[ true (always show ‘+’or ‘-‘)- or –

    false (never show ‘-‘ even if number is negative)

    – or –

    null (show ‘-‘ if number is negative)]

So in order to use this method we need to have the currency format in json. We can get the price format with the help of following code in magento.

       

Mage::helper('core')->jsonEncode(Mage::app()->getLocale()->getJsPriceFormat());

Here we can put the above code inside a method, which can be called in a .phtml file to print the price format in json.

Example :

       

public function getPriceFormat(){
        return Mage::helper('core')->jsonEncode(
                Mage::app()->getLocale()->getJsPriceFormat());
}

So in the .phtml file we can write it like

       

<script>var inst_price_format = <?php echo $this->getPriceFormat();?></script>


And now our job is done.

Finally we can call the formatCurrency() function by passing parameters as follows.

       

formatCurrency(105, inst_price_format)

This will give you the desired result.

AngularJS – A small introduction

AngularJS – A javascript framework which came to picture on 2012 when its first version i.e. Version 1.0 released. Now a days every one is focusing on dynamic web page applications, so keeping that in mind I found that this is one of the right tool to use. If you are looking for a single page application, then this is the best solution for you. Most of the credit for creating the framework goes to Miško Hevery (a google employee), who started to work on this project in 2009. After that a google development team has started to support this project. Now we have the latest version of this project i.e. v1.3.0-rc.3.

Do I need to care about certain points or do I need to know something about AngularJS before learning or implementing in any project?

  • This is a javascript framework which executes in the client end.
  • This is not a javascript library like jQuery.
  • This does not need help of jQuery to work. Both are different like jquery is a javascript library and AngularJS is a javascript framework.
  • jQuery can be used in a AngularJS application, depending on the application’s functionality.
  • This comes with MIT license, which means that we can use it without any cost or in other words we say that it is a open source technology.
  • AngularJS works with most of the modern browsers.
  • Having knowledge on html, css and javascript will really help you in your learning path as well as implementation path of AngularJS.
  • “ng-” is a most important prefix which AngularJS uses to identify its property.
  • In a simple way we can say that AngularJS directives are nothing but are extended html elements and differs with use a prefix i.e. “ng-”
  • Google cdn link for AngularJS :
    <script src=”//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js”></script>
  • AngularJS official site url :
    https://angularjs.org/
  • To use AngularJS frame work in a project, we do not need any special installation kind of stuff. Simply we need to include the AngularJS script source file in our project inside a script tag.

Can I get an Example?

       

<!DOCTYPE html>
<html>
	<head>
		<title>Simple Example On AngularJS</title>
	</head>
<body>
	<h1>AngularJS Example</h1>
	
	<div data-ng-app="" data-ng-init="guestComment=''">
		<textarea ng-model="guestComment" 
		placeholder="Enter your comment here" cols="60" rows="10"></textarea>
		<br />
		Your Comments :<br />
		<textarea cols="60" rows="10">{{guestComment}}</textarea>
	</div>
	<!-- Include AngularJS script file here //-->
	<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
</body>
</html>
       

So these are certain points which we should know before we start with AngularJS. I will update you more details about AngularJS and how we can use it to create some simple application in my upcoming blogs.

Thanks for your valuable time. Hope we will meet soon.

Stay Happy 🙂
Cheers!!

Is It A Friday?

Someday sometime we do not know, what is going on. A working day sometimes brings smile in our face and sometimes we think to avoid that. Sometimes we feel like we can do any thing and every thing and some times we ask thyself is it true?

Oh! It is Friday and lol we are out of our control. We are going to relax for next couple of days. Oh! It is Monday and all turns to hell. Why so?

Finding an answer is too difficult. But I guess we are missing something. So what is that?

HAPPINESS ( 🙂 )

Yes. One thing is sure friends, try to remain happy always. Everything and every time will pass slowly. Try to remain calm and happy. Then we will find every day as a Friday. Time is too short and words can not express all emotions of life always.

Remain Cool !! Stay Happy.

Have a nice Friday evening guys.

Cheers!!!