Liquid Globals in JS

Highlighted
New Member
2 0 0

 

Hello All,

 

My first ever question :-)

 

In my theme.js.liquid I am trying access a Liquid global "cart" object, such that when theme.js is created at serverside runtime it has access to the cart objects properties.  For instance, in my Javascript I need to do something like the following to grab the carts.currency.iso _code:

 

 

var displayPrice = variant.price;
var displayComparePrice = variant.compare_at_price;
var currency = '{{cart.currency.iso_code}}';
if (currency == 'AUD')
{
  displayPrice *= 1.1;
  displayComparePrice *= 1.1;
}

 

I have used this liquid object (and liquid code) perfectly fine in other places when static HTML pages are generated.

 

I have also tried to see if the cart object "exists" in JS world:

 

 

var currency = cart.currency.iso_code;

... to no avail.

 

 

TIA for any help.  It would be greatly appreciated.

 

Cheers,

Hymee

 

0 Likes
New Member
2 0 0

OK.  Thanks to the debugger in Chrome I found the global Shopify object, so the correct answer is:

 

var currency = Shopify.currency.active;

Of course it doesn't make sense to use Liquid at the point of .js creation, as it is "dynamic" value.

 

Cheers,

Hymee.

0 Likes